noa1305.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Support for ON Semiconductor NOA1305 ambient light sensor
  4. *
  5. * Copyright (C) 2016 Emcraft Systems
  6. * Copyright (C) 2019 Collabora Ltd.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/iio/iio.h>
  12. #include <linux/iio/sysfs.h>
  13. #include <linux/module.h>
  14. #include <linux/regmap.h>
  15. #include <linux/regulator/consumer.h>
  16. #define NOA1305_REG_POWER_CONTROL 0x0
  17. #define NOA1305_POWER_CONTROL_DOWN 0x00
  18. #define NOA1305_POWER_CONTROL_ON 0x08
  19. #define NOA1305_REG_RESET 0x1
  20. #define NOA1305_RESET_RESET 0x10
  21. #define NOA1305_REG_INTEGRATION_TIME 0x2
  22. #define NOA1305_INTEGR_TIME_800MS 0x00
  23. #define NOA1305_INTEGR_TIME_400MS 0x01
  24. #define NOA1305_INTEGR_TIME_200MS 0x02
  25. #define NOA1305_INTEGR_TIME_100MS 0x03
  26. #define NOA1305_INTEGR_TIME_50MS 0x04
  27. #define NOA1305_INTEGR_TIME_25MS 0x05
  28. #define NOA1305_INTEGR_TIME_12_5MS 0x06
  29. #define NOA1305_INTEGR_TIME_6_25MS 0x07
  30. #define NOA1305_REG_INT_SELECT 0x3
  31. #define NOA1305_INT_SEL_ACTIVE_HIGH 0x01
  32. #define NOA1305_INT_SEL_ACTIVE_LOW 0x02
  33. #define NOA1305_INT_SEL_INACTIVE 0x03
  34. #define NOA1305_REG_INT_THRESH_LSB 0x4
  35. #define NOA1305_REG_INT_THRESH_MSB 0x5
  36. #define NOA1305_REG_ALS_DATA_LSB 0x6
  37. #define NOA1305_REG_ALS_DATA_MSB 0x7
  38. #define NOA1305_REG_DEVICE_ID_LSB 0x8
  39. #define NOA1305_REG_DEVICE_ID_MSB 0x9
  40. #define NOA1305_DEVICE_ID 0x0519
  41. #define NOA1305_DRIVER_NAME "noa1305"
  42. struct noa1305_priv {
  43. struct i2c_client *client;
  44. struct regmap *regmap;
  45. struct regulator *vin_reg;
  46. };
  47. static int noa1305_measure(struct noa1305_priv *priv)
  48. {
  49. __le16 data;
  50. int ret;
  51. ret = regmap_bulk_read(priv->regmap, NOA1305_REG_ALS_DATA_LSB, &data,
  52. 2);
  53. if (ret < 0)
  54. return ret;
  55. return le16_to_cpu(data);
  56. }
  57. static int noa1305_scale(struct noa1305_priv *priv, int *val, int *val2)
  58. {
  59. int data;
  60. int ret;
  61. ret = regmap_read(priv->regmap, NOA1305_REG_INTEGRATION_TIME, &data);
  62. if (ret < 0)
  63. return ret;
  64. /*
  65. * Lux = count / (<Integration Constant> * <Integration Time>)
  66. *
  67. * Integration Constant = 7.7
  68. * Integration Time in Seconds
  69. */
  70. switch (data) {
  71. case NOA1305_INTEGR_TIME_800MS:
  72. *val = 100;
  73. *val2 = 77 * 8;
  74. break;
  75. case NOA1305_INTEGR_TIME_400MS:
  76. *val = 100;
  77. *val2 = 77 * 4;
  78. break;
  79. case NOA1305_INTEGR_TIME_200MS:
  80. *val = 100;
  81. *val2 = 77 * 2;
  82. break;
  83. case NOA1305_INTEGR_TIME_100MS:
  84. *val = 100;
  85. *val2 = 77;
  86. break;
  87. case NOA1305_INTEGR_TIME_50MS:
  88. *val = 1000;
  89. *val2 = 77 * 5;
  90. break;
  91. case NOA1305_INTEGR_TIME_25MS:
  92. *val = 10000;
  93. *val2 = 77 * 25;
  94. break;
  95. case NOA1305_INTEGR_TIME_12_5MS:
  96. *val = 100000;
  97. *val2 = 77 * 125;
  98. break;
  99. case NOA1305_INTEGR_TIME_6_25MS:
  100. *val = 1000000;
  101. *val2 = 77 * 625;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. return IIO_VAL_FRACTIONAL;
  107. }
  108. static const struct iio_chan_spec noa1305_channels[] = {
  109. {
  110. .type = IIO_LIGHT,
  111. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  112. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  113. }
  114. };
  115. static int noa1305_read_raw(struct iio_dev *indio_dev,
  116. struct iio_chan_spec const *chan,
  117. int *val, int *val2, long mask)
  118. {
  119. int ret = -EINVAL;
  120. struct noa1305_priv *priv = iio_priv(indio_dev);
  121. switch (mask) {
  122. case IIO_CHAN_INFO_RAW:
  123. switch (chan->type) {
  124. case IIO_LIGHT:
  125. ret = noa1305_measure(priv);
  126. if (ret < 0)
  127. return ret;
  128. *val = ret;
  129. return IIO_VAL_INT;
  130. default:
  131. break;
  132. }
  133. break;
  134. case IIO_CHAN_INFO_SCALE:
  135. switch (chan->type) {
  136. case IIO_LIGHT:
  137. return noa1305_scale(priv, val, val2);
  138. default:
  139. break;
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. return ret;
  146. }
  147. static const struct iio_info noa1305_info = {
  148. .read_raw = noa1305_read_raw,
  149. };
  150. static bool noa1305_writable_reg(struct device *dev, unsigned int reg)
  151. {
  152. switch (reg) {
  153. case NOA1305_REG_POWER_CONTROL:
  154. case NOA1305_REG_RESET:
  155. case NOA1305_REG_INTEGRATION_TIME:
  156. case NOA1305_REG_INT_SELECT:
  157. case NOA1305_REG_INT_THRESH_LSB:
  158. case NOA1305_REG_INT_THRESH_MSB:
  159. return true;
  160. default:
  161. return false;
  162. }
  163. }
  164. static const struct regmap_config noa1305_regmap_config = {
  165. .name = NOA1305_DRIVER_NAME,
  166. .reg_bits = 8,
  167. .val_bits = 8,
  168. .max_register = NOA1305_REG_DEVICE_ID_MSB,
  169. .writeable_reg = noa1305_writable_reg,
  170. };
  171. static void noa1305_reg_remove(void *data)
  172. {
  173. struct noa1305_priv *priv = data;
  174. regulator_disable(priv->vin_reg);
  175. }
  176. static int noa1305_probe(struct i2c_client *client,
  177. const struct i2c_device_id *id)
  178. {
  179. struct noa1305_priv *priv;
  180. struct iio_dev *indio_dev;
  181. struct regmap *regmap;
  182. __le16 data;
  183. unsigned int dev_id;
  184. int ret;
  185. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*priv));
  186. if (!indio_dev)
  187. return -ENOMEM;
  188. regmap = devm_regmap_init_i2c(client, &noa1305_regmap_config);
  189. if (IS_ERR(regmap)) {
  190. dev_err(&client->dev, "Regmap initialization failed.\n");
  191. return PTR_ERR(regmap);
  192. }
  193. priv = iio_priv(indio_dev);
  194. priv->vin_reg = devm_regulator_get(&client->dev, "vin");
  195. if (IS_ERR(priv->vin_reg))
  196. return dev_err_probe(&client->dev, PTR_ERR(priv->vin_reg),
  197. "get regulator vin failed\n");
  198. ret = regulator_enable(priv->vin_reg);
  199. if (ret) {
  200. dev_err(&client->dev, "enable regulator vin failed\n");
  201. return ret;
  202. }
  203. ret = devm_add_action_or_reset(&client->dev, noa1305_reg_remove, priv);
  204. if (ret) {
  205. dev_err(&client->dev, "addition of devm action failed\n");
  206. return ret;
  207. }
  208. i2c_set_clientdata(client, indio_dev);
  209. priv->client = client;
  210. priv->regmap = regmap;
  211. ret = regmap_bulk_read(regmap, NOA1305_REG_DEVICE_ID_LSB, &data, 2);
  212. if (ret < 0) {
  213. dev_err(&client->dev, "ID reading failed: %d\n", ret);
  214. return ret;
  215. }
  216. dev_id = le16_to_cpu(data);
  217. if (dev_id != NOA1305_DEVICE_ID) {
  218. dev_err(&client->dev, "Unknown device ID: 0x%x\n", dev_id);
  219. return -ENODEV;
  220. }
  221. ret = regmap_write(regmap, NOA1305_REG_POWER_CONTROL,
  222. NOA1305_POWER_CONTROL_ON);
  223. if (ret < 0) {
  224. dev_err(&client->dev, "Enabling power control failed\n");
  225. return ret;
  226. }
  227. ret = regmap_write(regmap, NOA1305_REG_RESET, NOA1305_RESET_RESET);
  228. if (ret < 0) {
  229. dev_err(&client->dev, "Device reset failed\n");
  230. return ret;
  231. }
  232. ret = regmap_write(regmap, NOA1305_REG_INTEGRATION_TIME,
  233. NOA1305_INTEGR_TIME_800MS);
  234. if (ret < 0) {
  235. dev_err(&client->dev, "Setting integration time failed\n");
  236. return ret;
  237. }
  238. indio_dev->info = &noa1305_info;
  239. indio_dev->channels = noa1305_channels;
  240. indio_dev->num_channels = ARRAY_SIZE(noa1305_channels);
  241. indio_dev->name = NOA1305_DRIVER_NAME;
  242. indio_dev->modes = INDIO_DIRECT_MODE;
  243. ret = devm_iio_device_register(&client->dev, indio_dev);
  244. if (ret)
  245. dev_err(&client->dev, "registering device failed\n");
  246. return ret;
  247. }
  248. static const struct of_device_id noa1305_of_match[] = {
  249. { .compatible = "onnn,noa1305" },
  250. { }
  251. };
  252. MODULE_DEVICE_TABLE(of, noa1305_of_match);
  253. static const struct i2c_device_id noa1305_ids[] = {
  254. { "noa1305", 0 },
  255. { }
  256. };
  257. MODULE_DEVICE_TABLE(i2c, noa1305_ids);
  258. static struct i2c_driver noa1305_driver = {
  259. .driver = {
  260. .name = NOA1305_DRIVER_NAME,
  261. .of_match_table = noa1305_of_match,
  262. },
  263. .probe = noa1305_probe,
  264. .id_table = noa1305_ids,
  265. };
  266. module_i2c_driver(noa1305_driver);
  267. MODULE_AUTHOR("Sergei Miroshnichenko <[email protected]>");
  268. MODULE_AUTHOR("Martyn Welch <[email protected]");
  269. MODULE_DESCRIPTION("ON Semiconductor NOA1305 ambient light sensor");
  270. MODULE_LICENSE("GPL");