bh1780.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ROHM 1780GLI Ambient Light Sensor Driver
  4. *
  5. * Copyright (C) 2016 Linaro Ltd.
  6. * Author: Linus Walleij <linus.walleij@linaro.org>
  7. * Loosely based on the previous BH1780 ALS misc driver
  8. * Copyright (C) 2010 Texas Instruments
  9. * Author: Hemanth V <hemanthv@ti.com>
  10. */
  11. #include <linux/i2c.h>
  12. #include <linux/slab.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/delay.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/bitops.h>
  21. #define BH1780_CMD_BIT BIT(7)
  22. #define BH1780_REG_CONTROL 0x00
  23. #define BH1780_REG_PARTID 0x0A
  24. #define BH1780_REG_MANFID 0x0B
  25. #define BH1780_REG_DLOW 0x0C
  26. #define BH1780_REG_DHIGH 0x0D
  27. #define BH1780_REVMASK GENMASK(3,0)
  28. #define BH1780_POWMASK GENMASK(1,0)
  29. #define BH1780_POFF (0x0)
  30. #define BH1780_PON (0x3)
  31. /* power on settling time in ms */
  32. #define BH1780_PON_DELAY 2
  33. /* max time before value available in ms */
  34. #define BH1780_INTERVAL 250
  35. struct bh1780_data {
  36. struct i2c_client *client;
  37. };
  38. static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val)
  39. {
  40. int ret = i2c_smbus_write_byte_data(bh1780->client,
  41. BH1780_CMD_BIT | reg,
  42. val);
  43. if (ret < 0)
  44. dev_err(&bh1780->client->dev,
  45. "i2c_smbus_write_byte_data failed error "
  46. "%d, register %01x\n",
  47. ret, reg);
  48. return ret;
  49. }
  50. static int bh1780_read(struct bh1780_data *bh1780, u8 reg)
  51. {
  52. int ret = i2c_smbus_read_byte_data(bh1780->client,
  53. BH1780_CMD_BIT | reg);
  54. if (ret < 0)
  55. dev_err(&bh1780->client->dev,
  56. "i2c_smbus_read_byte_data failed error "
  57. "%d, register %01x\n",
  58. ret, reg);
  59. return ret;
  60. }
  61. static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg)
  62. {
  63. int ret = i2c_smbus_read_word_data(bh1780->client,
  64. BH1780_CMD_BIT | reg);
  65. if (ret < 0)
  66. dev_err(&bh1780->client->dev,
  67. "i2c_smbus_read_word_data failed error "
  68. "%d, register %01x\n",
  69. ret, reg);
  70. return ret;
  71. }
  72. static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev,
  73. unsigned int reg, unsigned int writeval,
  74. unsigned int *readval)
  75. {
  76. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  77. int ret;
  78. if (!readval)
  79. return bh1780_write(bh1780, (u8)reg, (u8)writeval);
  80. ret = bh1780_read(bh1780, (u8)reg);
  81. if (ret < 0)
  82. return ret;
  83. *readval = ret;
  84. return 0;
  85. }
  86. static int bh1780_read_raw(struct iio_dev *indio_dev,
  87. struct iio_chan_spec const *chan,
  88. int *val, int *val2, long mask)
  89. {
  90. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  91. int value;
  92. switch (mask) {
  93. case IIO_CHAN_INFO_RAW:
  94. switch (chan->type) {
  95. case IIO_LIGHT:
  96. pm_runtime_get_sync(&bh1780->client->dev);
  97. value = bh1780_read_word(bh1780, BH1780_REG_DLOW);
  98. if (value < 0)
  99. return value;
  100. pm_runtime_mark_last_busy(&bh1780->client->dev);
  101. pm_runtime_put_autosuspend(&bh1780->client->dev);
  102. *val = value;
  103. return IIO_VAL_INT;
  104. default:
  105. return -EINVAL;
  106. }
  107. case IIO_CHAN_INFO_INT_TIME:
  108. *val = 0;
  109. *val2 = BH1780_INTERVAL * 1000;
  110. return IIO_VAL_INT_PLUS_MICRO;
  111. default:
  112. return -EINVAL;
  113. }
  114. }
  115. static const struct iio_info bh1780_info = {
  116. .read_raw = bh1780_read_raw,
  117. .debugfs_reg_access = bh1780_debugfs_reg_access,
  118. };
  119. static const struct iio_chan_spec bh1780_channels[] = {
  120. {
  121. .type = IIO_LIGHT,
  122. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  123. BIT(IIO_CHAN_INFO_INT_TIME)
  124. }
  125. };
  126. static int bh1780_probe(struct i2c_client *client)
  127. {
  128. int ret;
  129. struct bh1780_data *bh1780;
  130. struct i2c_adapter *adapter = client->adapter;
  131. struct iio_dev *indio_dev;
  132. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  133. return -EIO;
  134. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780));
  135. if (!indio_dev)
  136. return -ENOMEM;
  137. bh1780 = iio_priv(indio_dev);
  138. bh1780->client = client;
  139. i2c_set_clientdata(client, indio_dev);
  140. /* Power up the device */
  141. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
  142. if (ret < 0)
  143. return ret;
  144. msleep(BH1780_PON_DELAY);
  145. pm_runtime_get_noresume(&client->dev);
  146. pm_runtime_set_active(&client->dev);
  147. pm_runtime_enable(&client->dev);
  148. ret = bh1780_read(bh1780, BH1780_REG_PARTID);
  149. if (ret < 0)
  150. goto out_disable_pm;
  151. dev_info(&client->dev,
  152. "Ambient Light Sensor, Rev : %lu\n",
  153. (ret & BH1780_REVMASK));
  154. /*
  155. * As the device takes 250 ms to even come up with a fresh
  156. * measurement after power-on, do not shut it down unnecessarily.
  157. * Set autosuspend to a five seconds.
  158. */
  159. pm_runtime_set_autosuspend_delay(&client->dev, 5000);
  160. pm_runtime_use_autosuspend(&client->dev);
  161. pm_runtime_put(&client->dev);
  162. indio_dev->info = &bh1780_info;
  163. indio_dev->name = "bh1780";
  164. indio_dev->channels = bh1780_channels;
  165. indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
  166. indio_dev->modes = INDIO_DIRECT_MODE;
  167. ret = iio_device_register(indio_dev);
  168. if (ret)
  169. goto out_disable_pm;
  170. return 0;
  171. out_disable_pm:
  172. pm_runtime_put_noidle(&client->dev);
  173. pm_runtime_disable(&client->dev);
  174. return ret;
  175. }
  176. static void bh1780_remove(struct i2c_client *client)
  177. {
  178. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  179. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  180. int ret;
  181. iio_device_unregister(indio_dev);
  182. pm_runtime_get_sync(&client->dev);
  183. pm_runtime_put_noidle(&client->dev);
  184. pm_runtime_disable(&client->dev);
  185. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
  186. if (ret < 0)
  187. dev_err(&client->dev, "failed to power off (%pe)\n",
  188. ERR_PTR(ret));
  189. }
  190. static int bh1780_runtime_suspend(struct device *dev)
  191. {
  192. struct i2c_client *client = to_i2c_client(dev);
  193. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  194. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  195. int ret;
  196. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
  197. if (ret < 0) {
  198. dev_err(dev, "failed to runtime suspend\n");
  199. return ret;
  200. }
  201. return 0;
  202. }
  203. static int bh1780_runtime_resume(struct device *dev)
  204. {
  205. struct i2c_client *client = to_i2c_client(dev);
  206. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  207. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  208. int ret;
  209. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
  210. if (ret < 0) {
  211. dev_err(dev, "failed to runtime resume\n");
  212. return ret;
  213. }
  214. /* Wait for power on, then for a value to be available */
  215. msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
  216. return 0;
  217. }
  218. static DEFINE_RUNTIME_DEV_PM_OPS(bh1780_dev_pm_ops, bh1780_runtime_suspend,
  219. bh1780_runtime_resume, NULL);
  220. static const struct i2c_device_id bh1780_id[] = {
  221. { "bh1780" },
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(i2c, bh1780_id);
  225. static const struct of_device_id of_bh1780_match[] = {
  226. { .compatible = "rohm,bh1780gli", },
  227. {},
  228. };
  229. MODULE_DEVICE_TABLE(of, of_bh1780_match);
  230. static struct i2c_driver bh1780_driver = {
  231. .probe = bh1780_probe,
  232. .remove = bh1780_remove,
  233. .id_table = bh1780_id,
  234. .driver = {
  235. .name = "bh1780",
  236. .pm = pm_ptr(&bh1780_dev_pm_ops),
  237. .of_match_table = of_bh1780_match,
  238. },
  239. };
  240. module_i2c_driver(bh1780_driver);
  241. MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
  242. MODULE_LICENSE("GPL");
  243. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");