bh1780.c 7.2 KB

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