al3010.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AL3010 - Dyna Image Ambient Light Sensor
  4. *
  5. * Copyright (c) 2014, Intel Corporation.
  6. * Copyright (c) 2016, Dyna-Image Corp.
  7. * Copyright (c) 2020, David Heidelberg, Michał Mirosław, Dmitry Osipenko
  8. *
  9. * IIO driver for AL3010 (7-bit I2C slave address 0x1C).
  10. *
  11. * TODO: interrupt support, thresholds
  12. * When the driver will get support for interrupt handling, then interrupt
  13. * will need to be disabled before turning sensor OFF in order to avoid
  14. * potential races with the interrupt handling.
  15. */
  16. #include <linux/bitfield.h>
  17. #include <linux/i2c.h>
  18. #include <linux/module.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #define AL3010_DRV_NAME "al3010"
  23. #define AL3010_REG_SYSTEM 0x00
  24. #define AL3010_REG_DATA_LOW 0x0c
  25. #define AL3010_REG_CONFIG 0x10
  26. #define AL3010_CONFIG_DISABLE 0x00
  27. #define AL3010_CONFIG_ENABLE 0x01
  28. #define AL3010_GAIN_MASK GENMASK(6,4)
  29. #define AL3010_SCALE_AVAILABLE "1.1872 0.2968 0.0742 0.018"
  30. enum al3xxxx_range {
  31. AL3XXX_RANGE_1, /* 77806 lx */
  32. AL3XXX_RANGE_2, /* 19542 lx */
  33. AL3XXX_RANGE_3, /* 4863 lx */
  34. AL3XXX_RANGE_4 /* 1216 lx */
  35. };
  36. static const int al3010_scales[][2] = {
  37. {0, 1187200}, {0, 296800}, {0, 74200}, {0, 18600}
  38. };
  39. struct al3010_data {
  40. struct i2c_client *client;
  41. };
  42. static const struct iio_chan_spec al3010_channels[] = {
  43. {
  44. .type = IIO_LIGHT,
  45. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  46. BIT(IIO_CHAN_INFO_SCALE),
  47. }
  48. };
  49. static IIO_CONST_ATTR(in_illuminance_scale_available, AL3010_SCALE_AVAILABLE);
  50. static struct attribute *al3010_attributes[] = {
  51. &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
  52. NULL,
  53. };
  54. static const struct attribute_group al3010_attribute_group = {
  55. .attrs = al3010_attributes,
  56. };
  57. static int al3010_set_pwr(struct i2c_client *client, bool pwr)
  58. {
  59. u8 val = pwr ? AL3010_CONFIG_ENABLE : AL3010_CONFIG_DISABLE;
  60. return i2c_smbus_write_byte_data(client, AL3010_REG_SYSTEM, val);
  61. }
  62. static void al3010_set_pwr_off(void *_data)
  63. {
  64. struct al3010_data *data = _data;
  65. al3010_set_pwr(data->client, false);
  66. }
  67. static int al3010_init(struct al3010_data *data)
  68. {
  69. int ret;
  70. ret = al3010_set_pwr(data->client, true);
  71. if (ret < 0)
  72. return ret;
  73. ret = devm_add_action_or_reset(&data->client->dev,
  74. al3010_set_pwr_off,
  75. data);
  76. if (ret < 0)
  77. return ret;
  78. ret = i2c_smbus_write_byte_data(data->client, AL3010_REG_CONFIG,
  79. FIELD_PREP(AL3010_GAIN_MASK,
  80. AL3XXX_RANGE_3));
  81. if (ret < 0)
  82. return ret;
  83. return 0;
  84. }
  85. static int al3010_read_raw(struct iio_dev *indio_dev,
  86. struct iio_chan_spec const *chan, int *val,
  87. int *val2, long mask)
  88. {
  89. struct al3010_data *data = iio_priv(indio_dev);
  90. int ret;
  91. switch (mask) {
  92. case IIO_CHAN_INFO_RAW:
  93. /*
  94. * ALS ADC value is stored in two adjacent registers:
  95. * - low byte of output is stored at AL3010_REG_DATA_LOW
  96. * - high byte of output is stored at AL3010_REG_DATA_LOW + 1
  97. */
  98. ret = i2c_smbus_read_word_data(data->client,
  99. AL3010_REG_DATA_LOW);
  100. if (ret < 0)
  101. return ret;
  102. *val = ret;
  103. return IIO_VAL_INT;
  104. case IIO_CHAN_INFO_SCALE:
  105. ret = i2c_smbus_read_byte_data(data->client,
  106. AL3010_REG_CONFIG);
  107. if (ret < 0)
  108. return ret;
  109. ret = FIELD_GET(AL3010_GAIN_MASK, ret);
  110. *val = al3010_scales[ret][0];
  111. *val2 = al3010_scales[ret][1];
  112. return IIO_VAL_INT_PLUS_MICRO;
  113. }
  114. return -EINVAL;
  115. }
  116. static int al3010_write_raw(struct iio_dev *indio_dev,
  117. struct iio_chan_spec const *chan, int val,
  118. int val2, long mask)
  119. {
  120. struct al3010_data *data = iio_priv(indio_dev);
  121. int i;
  122. switch (mask) {
  123. case IIO_CHAN_INFO_SCALE:
  124. for (i = 0; i < ARRAY_SIZE(al3010_scales); i++) {
  125. if (val != al3010_scales[i][0] ||
  126. val2 != al3010_scales[i][1])
  127. continue;
  128. return i2c_smbus_write_byte_data(data->client,
  129. AL3010_REG_CONFIG,
  130. FIELD_PREP(AL3010_GAIN_MASK, i));
  131. }
  132. break;
  133. }
  134. return -EINVAL;
  135. }
  136. static const struct iio_info al3010_info = {
  137. .read_raw = al3010_read_raw,
  138. .write_raw = al3010_write_raw,
  139. .attrs = &al3010_attribute_group,
  140. };
  141. static int al3010_probe(struct i2c_client *client)
  142. {
  143. struct al3010_data *data;
  144. struct iio_dev *indio_dev;
  145. int ret;
  146. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  147. if (!indio_dev)
  148. return -ENOMEM;
  149. data = iio_priv(indio_dev);
  150. i2c_set_clientdata(client, indio_dev);
  151. data->client = client;
  152. indio_dev->info = &al3010_info;
  153. indio_dev->name = AL3010_DRV_NAME;
  154. indio_dev->channels = al3010_channels;
  155. indio_dev->num_channels = ARRAY_SIZE(al3010_channels);
  156. indio_dev->modes = INDIO_DIRECT_MODE;
  157. ret = al3010_init(data);
  158. if (ret < 0) {
  159. dev_err(&client->dev, "al3010 chip init failed\n");
  160. return ret;
  161. }
  162. return devm_iio_device_register(&client->dev, indio_dev);
  163. }
  164. static int al3010_suspend(struct device *dev)
  165. {
  166. return al3010_set_pwr(to_i2c_client(dev), false);
  167. }
  168. static int al3010_resume(struct device *dev)
  169. {
  170. return al3010_set_pwr(to_i2c_client(dev), true);
  171. }
  172. static DEFINE_SIMPLE_DEV_PM_OPS(al3010_pm_ops, al3010_suspend, al3010_resume);
  173. static const struct i2c_device_id al3010_id[] = {
  174. {"al3010", },
  175. {}
  176. };
  177. MODULE_DEVICE_TABLE(i2c, al3010_id);
  178. static const struct of_device_id al3010_of_match[] = {
  179. { .compatible = "dynaimage,al3010", },
  180. {},
  181. };
  182. MODULE_DEVICE_TABLE(of, al3010_of_match);
  183. static struct i2c_driver al3010_driver = {
  184. .driver = {
  185. .name = AL3010_DRV_NAME,
  186. .of_match_table = al3010_of_match,
  187. .pm = pm_sleep_ptr(&al3010_pm_ops),
  188. },
  189. .probe = al3010_probe,
  190. .id_table = al3010_id,
  191. };
  192. module_i2c_driver(al3010_driver);
  193. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@nxp.com>");
  194. MODULE_AUTHOR("David Heidelberg <david@ixit.cz>");
  195. MODULE_DESCRIPTION("AL3010 Ambient Light Sensor driver");
  196. MODULE_LICENSE("GPL v2");