bh1780.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <[email protected]>
  7. * Loosely based on the previous BH1780 ALS misc driver
  8. * Copyright (C) 2010 Texas Instruments
  9. * Author: Hemanth V <[email protected]>
  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. const struct i2c_device_id *id)
  128. {
  129. int ret;
  130. struct bh1780_data *bh1780;
  131. struct i2c_adapter *adapter = client->adapter;
  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->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 void 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 (%pe)\n",
  189. ERR_PTR(ret));
  190. }
  191. static int bh1780_runtime_suspend(struct device *dev)
  192. {
  193. struct i2c_client *client = to_i2c_client(dev);
  194. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  195. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  196. int ret;
  197. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
  198. if (ret < 0) {
  199. dev_err(dev, "failed to runtime suspend\n");
  200. return ret;
  201. }
  202. return 0;
  203. }
  204. static int bh1780_runtime_resume(struct device *dev)
  205. {
  206. struct i2c_client *client = to_i2c_client(dev);
  207. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  208. struct bh1780_data *bh1780 = iio_priv(indio_dev);
  209. int ret;
  210. ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
  211. if (ret < 0) {
  212. dev_err(dev, "failed to runtime resume\n");
  213. return ret;
  214. }
  215. /* Wait for power on, then for a value to be available */
  216. msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
  217. return 0;
  218. }
  219. static DEFINE_RUNTIME_DEV_PM_OPS(bh1780_dev_pm_ops, bh1780_runtime_suspend,
  220. bh1780_runtime_resume, NULL);
  221. static const struct i2c_device_id bh1780_id[] = {
  222. { "bh1780", 0 },
  223. { },
  224. };
  225. MODULE_DEVICE_TABLE(i2c, bh1780_id);
  226. static const struct of_device_id of_bh1780_match[] = {
  227. { .compatible = "rohm,bh1780gli", },
  228. {},
  229. };
  230. MODULE_DEVICE_TABLE(of, of_bh1780_match);
  231. static struct i2c_driver bh1780_driver = {
  232. .probe = bh1780_probe,
  233. .remove = bh1780_remove,
  234. .id_table = bh1780_id,
  235. .driver = {
  236. .name = "bh1780",
  237. .pm = pm_ptr(&bh1780_dev_pm_ops),
  238. .of_match_table = of_bh1780_match,
  239. },
  240. };
  241. module_i2c_driver(bh1780_driver);
  242. MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
  243. MODULE_LICENSE("GPL");
  244. MODULE_AUTHOR("Linus Walleij <[email protected]>");