bh1780.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. .read_raw = bh1780_read_raw,
  116. .debugfs_reg_access = bh1780_debugfs_reg_access,
  117. };
  118. static const struct iio_chan_spec bh1780_channels[] = {
  119. {
  120. .type = IIO_LIGHT,
  121. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  122. BIT(IIO_CHAN_INFO_INT_TIME)
  123. }
  124. };
  125. static int bh1780_probe(struct i2c_client *client,
  126. const struct i2c_device_id *id)
  127. {
  128. int ret;
  129. struct bh1780_data *bh1780;
  130. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  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->dev.parent = &client->dev;
  163. indio_dev->info = &bh1780_info;
  164. indio_dev->name = "bh1780";
  165. indio_dev->channels = bh1780_channels;
  166. indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
  167. indio_dev->modes = INDIO_DIRECT_MODE;
  168. ret = iio_device_register(indio_dev);
  169. if (ret)
  170. goto out_disable_pm;
  171. return 0;
  172. out_disable_pm:
  173. pm_runtime_put_noidle(&client->dev);
  174. pm_runtime_disable(&client->dev);
  175. return ret;
  176. }
  177. static int bh1780_remove(struct i2c_client *client)
  178. {
  179. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  180. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  181. int ret;
  182. iio_device_unregister(indio_dev);
  183. pm_runtime_get_sync(&client->dev);
  184. pm_runtime_put_noidle(&client->dev);
  185. pm_runtime_disable(&client->dev);
  186. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
  187. if (ret < 0) {
  188. dev_err(&client->dev, "failed to power off\n");
  189. return ret;
  190. }
  191. return 0;
  192. }
  193. #ifdef CONFIG_PM
  194. static int bh1780_runtime_suspend(struct device *dev)
  195. {
  196. struct i2c_client *client = to_i2c_client(dev);
  197. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  198. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  199. int ret;
  200. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
  201. if (ret < 0) {
  202. dev_err(dev, "failed to runtime suspend\n");
  203. return ret;
  204. }
  205. return 0;
  206. }
  207. static int bh1780_runtime_resume(struct device *dev)
  208. {
  209. struct i2c_client *client = to_i2c_client(dev);
  210. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  211. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  212. int ret;
  213. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
  214. if (ret < 0) {
  215. dev_err(dev, "failed to runtime resume\n");
  216. return ret;
  217. }
  218. /* Wait for power on, then for a value to be available */
  219. msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
  220. return 0;
  221. }
  222. #endif /* CONFIG_PM */
  223. static const struct dev_pm_ops bh1780_dev_pm_ops = {
  224. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  225. pm_runtime_force_resume)
  226. SET_RUNTIME_PM_OPS(bh1780_runtime_suspend,
  227. bh1780_runtime_resume, NULL)
  228. };
  229. static const struct i2c_device_id bh1780_id[] = {
  230. { "bh1780", 0 },
  231. { },
  232. };
  233. MODULE_DEVICE_TABLE(i2c, bh1780_id);
  234. #ifdef CONFIG_OF
  235. static const struct of_device_id of_bh1780_match[] = {
  236. { .compatible = "rohm,bh1780gli", },
  237. {},
  238. };
  239. MODULE_DEVICE_TABLE(of, of_bh1780_match);
  240. #endif
  241. static struct i2c_driver bh1780_driver = {
  242. .probe = bh1780_probe,
  243. .remove = bh1780_remove,
  244. .id_table = bh1780_id,
  245. .driver = {
  246. .name = "bh1780",
  247. .pm = &bh1780_dev_pm_ops,
  248. .of_match_table = of_match_ptr(of_bh1780_match),
  249. },
  250. };
  251. module_i2c_driver(bh1780_driver);
  252. MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
  253. MODULE_LICENSE("GPL");
  254. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");