bh1750.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensor driver
  4. *
  5. * Copyright (c) Tomasz Duszynski <[email protected]>
  6. *
  7. * Data sheets:
  8. * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1710fvc-e.pdf
  9. * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1715fvc-e.pdf
  10. * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1721fvc-e.pdf
  11. * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
  12. * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1751fvi-e.pdf
  13. *
  14. * 7-bit I2C slave addresses:
  15. * 0x23 (ADDR pin low)
  16. * 0x5C (ADDR pin high)
  17. *
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/i2c.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/module.h>
  24. #define BH1750_POWER_DOWN 0x00
  25. #define BH1750_ONE_TIME_H_RES_MODE 0x20 /* auto-mode for BH1721 */
  26. #define BH1750_CHANGE_INT_TIME_H_BIT 0x40
  27. #define BH1750_CHANGE_INT_TIME_L_BIT 0x60
  28. enum {
  29. BH1710,
  30. BH1721,
  31. BH1750,
  32. };
  33. struct bh1750_chip_info;
  34. struct bh1750_data {
  35. struct i2c_client *client;
  36. struct mutex lock;
  37. const struct bh1750_chip_info *chip_info;
  38. u16 mtreg;
  39. };
  40. struct bh1750_chip_info {
  41. u16 mtreg_min;
  42. u16 mtreg_max;
  43. u16 mtreg_default;
  44. int mtreg_to_usec;
  45. int mtreg_to_scale;
  46. /*
  47. * For BH1710/BH1721 all possible integration time values won't fit
  48. * into one page so displaying is limited to every second one.
  49. * Note, that user can still write proper values which were not
  50. * listed.
  51. */
  52. int inc;
  53. u16 int_time_low_mask;
  54. u16 int_time_high_mask;
  55. };
  56. static const struct bh1750_chip_info bh1750_chip_info_tbl[] = {
  57. [BH1710] = { 140, 1022, 300, 400, 250000000, 2, 0x001F, 0x03E0 },
  58. [BH1721] = { 140, 1020, 300, 400, 250000000, 2, 0x0010, 0x03E0 },
  59. [BH1750] = { 31, 254, 69, 1740, 57500000, 1, 0x001F, 0x00E0 },
  60. };
  61. static int bh1750_change_int_time(struct bh1750_data *data, int usec)
  62. {
  63. int ret;
  64. u16 val;
  65. u8 regval;
  66. const struct bh1750_chip_info *chip_info = data->chip_info;
  67. if ((usec % chip_info->mtreg_to_usec) != 0)
  68. return -EINVAL;
  69. val = usec / chip_info->mtreg_to_usec;
  70. if (val < chip_info->mtreg_min || val > chip_info->mtreg_max)
  71. return -EINVAL;
  72. ret = i2c_smbus_write_byte(data->client, BH1750_POWER_DOWN);
  73. if (ret < 0)
  74. return ret;
  75. regval = (val & chip_info->int_time_high_mask) >> 5;
  76. ret = i2c_smbus_write_byte(data->client,
  77. BH1750_CHANGE_INT_TIME_H_BIT | regval);
  78. if (ret < 0)
  79. return ret;
  80. regval = val & chip_info->int_time_low_mask;
  81. ret = i2c_smbus_write_byte(data->client,
  82. BH1750_CHANGE_INT_TIME_L_BIT | regval);
  83. if (ret < 0)
  84. return ret;
  85. data->mtreg = val;
  86. return 0;
  87. }
  88. static int bh1750_read(struct bh1750_data *data, int *val)
  89. {
  90. int ret;
  91. __be16 result;
  92. const struct bh1750_chip_info *chip_info = data->chip_info;
  93. unsigned long delay = chip_info->mtreg_to_usec * data->mtreg;
  94. /*
  95. * BH1721 will enter continuous mode on receiving this command.
  96. * Note, that this eliminates need for bh1750_resume().
  97. */
  98. ret = i2c_smbus_write_byte(data->client, BH1750_ONE_TIME_H_RES_MODE);
  99. if (ret < 0)
  100. return ret;
  101. usleep_range(delay + 15000, delay + 40000);
  102. ret = i2c_master_recv(data->client, (char *)&result, 2);
  103. if (ret < 0)
  104. return ret;
  105. *val = be16_to_cpu(result);
  106. return 0;
  107. }
  108. static int bh1750_read_raw(struct iio_dev *indio_dev,
  109. struct iio_chan_spec const *chan,
  110. int *val, int *val2, long mask)
  111. {
  112. int ret, tmp;
  113. struct bh1750_data *data = iio_priv(indio_dev);
  114. const struct bh1750_chip_info *chip_info = data->chip_info;
  115. switch (mask) {
  116. case IIO_CHAN_INFO_RAW:
  117. switch (chan->type) {
  118. case IIO_LIGHT:
  119. mutex_lock(&data->lock);
  120. ret = bh1750_read(data, val);
  121. mutex_unlock(&data->lock);
  122. if (ret < 0)
  123. return ret;
  124. return IIO_VAL_INT;
  125. default:
  126. return -EINVAL;
  127. }
  128. case IIO_CHAN_INFO_SCALE:
  129. tmp = chip_info->mtreg_to_scale / data->mtreg;
  130. *val = tmp / 1000000;
  131. *val2 = tmp % 1000000;
  132. return IIO_VAL_INT_PLUS_MICRO;
  133. case IIO_CHAN_INFO_INT_TIME:
  134. *val = 0;
  135. *val2 = chip_info->mtreg_to_usec * data->mtreg;
  136. return IIO_VAL_INT_PLUS_MICRO;
  137. default:
  138. return -EINVAL;
  139. }
  140. }
  141. static int bh1750_write_raw(struct iio_dev *indio_dev,
  142. struct iio_chan_spec const *chan,
  143. int val, int val2, long mask)
  144. {
  145. int ret;
  146. struct bh1750_data *data = iio_priv(indio_dev);
  147. switch (mask) {
  148. case IIO_CHAN_INFO_INT_TIME:
  149. if (val != 0)
  150. return -EINVAL;
  151. mutex_lock(&data->lock);
  152. ret = bh1750_change_int_time(data, val2);
  153. mutex_unlock(&data->lock);
  154. return ret;
  155. default:
  156. return -EINVAL;
  157. }
  158. }
  159. static ssize_t bh1750_show_int_time_available(struct device *dev,
  160. struct device_attribute *attr, char *buf)
  161. {
  162. int i;
  163. size_t len = 0;
  164. struct bh1750_data *data = iio_priv(dev_to_iio_dev(dev));
  165. const struct bh1750_chip_info *chip_info = data->chip_info;
  166. for (i = chip_info->mtreg_min; i <= chip_info->mtreg_max; i += chip_info->inc)
  167. len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06d ",
  168. chip_info->mtreg_to_usec * i);
  169. buf[len - 1] = '\n';
  170. return len;
  171. }
  172. static IIO_DEV_ATTR_INT_TIME_AVAIL(bh1750_show_int_time_available);
  173. static struct attribute *bh1750_attributes[] = {
  174. &iio_dev_attr_integration_time_available.dev_attr.attr,
  175. NULL,
  176. };
  177. static const struct attribute_group bh1750_attribute_group = {
  178. .attrs = bh1750_attributes,
  179. };
  180. static const struct iio_info bh1750_info = {
  181. .attrs = &bh1750_attribute_group,
  182. .read_raw = bh1750_read_raw,
  183. .write_raw = bh1750_write_raw,
  184. };
  185. static const struct iio_chan_spec bh1750_channels[] = {
  186. {
  187. .type = IIO_LIGHT,
  188. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  189. BIT(IIO_CHAN_INFO_SCALE) |
  190. BIT(IIO_CHAN_INFO_INT_TIME)
  191. }
  192. };
  193. static int bh1750_probe(struct i2c_client *client,
  194. const struct i2c_device_id *id)
  195. {
  196. int ret, usec;
  197. struct bh1750_data *data;
  198. struct iio_dev *indio_dev;
  199. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  200. I2C_FUNC_SMBUS_WRITE_BYTE))
  201. return -EOPNOTSUPP;
  202. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  203. if (!indio_dev)
  204. return -ENOMEM;
  205. data = iio_priv(indio_dev);
  206. i2c_set_clientdata(client, indio_dev);
  207. data->client = client;
  208. data->chip_info = &bh1750_chip_info_tbl[id->driver_data];
  209. usec = data->chip_info->mtreg_to_usec * data->chip_info->mtreg_default;
  210. ret = bh1750_change_int_time(data, usec);
  211. if (ret < 0)
  212. return ret;
  213. mutex_init(&data->lock);
  214. indio_dev->info = &bh1750_info;
  215. indio_dev->name = id->name;
  216. indio_dev->channels = bh1750_channels;
  217. indio_dev->num_channels = ARRAY_SIZE(bh1750_channels);
  218. indio_dev->modes = INDIO_DIRECT_MODE;
  219. return iio_device_register(indio_dev);
  220. }
  221. static void bh1750_remove(struct i2c_client *client)
  222. {
  223. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  224. struct bh1750_data *data = iio_priv(indio_dev);
  225. iio_device_unregister(indio_dev);
  226. mutex_lock(&data->lock);
  227. i2c_smbus_write_byte(client, BH1750_POWER_DOWN);
  228. mutex_unlock(&data->lock);
  229. }
  230. static int bh1750_suspend(struct device *dev)
  231. {
  232. int ret;
  233. struct bh1750_data *data =
  234. iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  235. /*
  236. * This is mainly for BH1721 which doesn't enter power down
  237. * mode automatically.
  238. */
  239. mutex_lock(&data->lock);
  240. ret = i2c_smbus_write_byte(data->client, BH1750_POWER_DOWN);
  241. mutex_unlock(&data->lock);
  242. return ret;
  243. }
  244. static DEFINE_SIMPLE_DEV_PM_OPS(bh1750_pm_ops, bh1750_suspend, NULL);
  245. static const struct i2c_device_id bh1750_id[] = {
  246. { "bh1710", BH1710 },
  247. { "bh1715", BH1750 },
  248. { "bh1721", BH1721 },
  249. { "bh1750", BH1750 },
  250. { "bh1751", BH1750 },
  251. { }
  252. };
  253. MODULE_DEVICE_TABLE(i2c, bh1750_id);
  254. static const struct of_device_id bh1750_of_match[] = {
  255. { .compatible = "rohm,bh1710", },
  256. { .compatible = "rohm,bh1715", },
  257. { .compatible = "rohm,bh1721", },
  258. { .compatible = "rohm,bh1750", },
  259. { .compatible = "rohm,bh1751", },
  260. { }
  261. };
  262. MODULE_DEVICE_TABLE(of, bh1750_of_match);
  263. static struct i2c_driver bh1750_driver = {
  264. .driver = {
  265. .name = "bh1750",
  266. .of_match_table = bh1750_of_match,
  267. .pm = pm_sleep_ptr(&bh1750_pm_ops),
  268. },
  269. .probe = bh1750_probe,
  270. .remove = bh1750_remove,
  271. .id_table = bh1750_id,
  272. };
  273. module_i2c_driver(bh1750_driver);
  274. MODULE_AUTHOR("Tomasz Duszynski <[email protected]>");
  275. MODULE_DESCRIPTION("ROHM BH1710/BH1715/BH1721/BH1750/BH1751 als driver");
  276. MODULE_LICENSE("GPL v2");