al3010.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/of.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 = i2c_smbus_write_byte_data(data->client, AL3010_REG_CONFIG,
  74. FIELD_PREP(AL3010_GAIN_MASK,
  75. AL3XXX_RANGE_3));
  76. if (ret < 0)
  77. return ret;
  78. return 0;
  79. }
  80. static int al3010_read_raw(struct iio_dev *indio_dev,
  81. struct iio_chan_spec const *chan, int *val,
  82. int *val2, long mask)
  83. {
  84. struct al3010_data *data = iio_priv(indio_dev);
  85. int ret;
  86. switch (mask) {
  87. case IIO_CHAN_INFO_RAW:
  88. /*
  89. * ALS ADC value is stored in two adjacent registers:
  90. * - low byte of output is stored at AL3010_REG_DATA_LOW
  91. * - high byte of output is stored at AL3010_REG_DATA_LOW + 1
  92. */
  93. ret = i2c_smbus_read_word_data(data->client,
  94. AL3010_REG_DATA_LOW);
  95. if (ret < 0)
  96. return ret;
  97. *val = ret;
  98. return IIO_VAL_INT;
  99. case IIO_CHAN_INFO_SCALE:
  100. ret = i2c_smbus_read_byte_data(data->client,
  101. AL3010_REG_CONFIG);
  102. if (ret < 0)
  103. return ret;
  104. ret = FIELD_GET(AL3010_GAIN_MASK, ret);
  105. *val = al3010_scales[ret][0];
  106. *val2 = al3010_scales[ret][1];
  107. return IIO_VAL_INT_PLUS_MICRO;
  108. }
  109. return -EINVAL;
  110. }
  111. static int al3010_write_raw(struct iio_dev *indio_dev,
  112. struct iio_chan_spec const *chan, int val,
  113. int val2, long mask)
  114. {
  115. struct al3010_data *data = iio_priv(indio_dev);
  116. int i;
  117. switch (mask) {
  118. case IIO_CHAN_INFO_SCALE:
  119. for (i = 0; i < ARRAY_SIZE(al3010_scales); i++) {
  120. if (val != al3010_scales[i][0] ||
  121. val2 != al3010_scales[i][1])
  122. continue;
  123. return i2c_smbus_write_byte_data(data->client,
  124. AL3010_REG_CONFIG,
  125. FIELD_PREP(AL3010_GAIN_MASK, i));
  126. }
  127. break;
  128. }
  129. return -EINVAL;
  130. }
  131. static const struct iio_info al3010_info = {
  132. .read_raw = al3010_read_raw,
  133. .write_raw = al3010_write_raw,
  134. .attrs = &al3010_attribute_group,
  135. };
  136. static int al3010_probe(struct i2c_client *client,
  137. const struct i2c_device_id *id)
  138. {
  139. struct al3010_data *data;
  140. struct iio_dev *indio_dev;
  141. int ret;
  142. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  143. if (!indio_dev)
  144. return -ENOMEM;
  145. data = iio_priv(indio_dev);
  146. i2c_set_clientdata(client, indio_dev);
  147. data->client = client;
  148. indio_dev->info = &al3010_info;
  149. indio_dev->name = AL3010_DRV_NAME;
  150. indio_dev->channels = al3010_channels;
  151. indio_dev->num_channels = ARRAY_SIZE(al3010_channels);
  152. indio_dev->modes = INDIO_DIRECT_MODE;
  153. ret = al3010_init(data);
  154. if (ret < 0) {
  155. dev_err(&client->dev, "al3010 chip init failed\n");
  156. return ret;
  157. }
  158. ret = devm_add_action_or_reset(&client->dev,
  159. al3010_set_pwr_off,
  160. data);
  161. if (ret < 0)
  162. return ret;
  163. return devm_iio_device_register(&client->dev, indio_dev);
  164. }
  165. static int al3010_suspend(struct device *dev)
  166. {
  167. return al3010_set_pwr(to_i2c_client(dev), false);
  168. }
  169. static int al3010_resume(struct device *dev)
  170. {
  171. return al3010_set_pwr(to_i2c_client(dev), true);
  172. }
  173. static DEFINE_SIMPLE_DEV_PM_OPS(al3010_pm_ops, al3010_suspend, al3010_resume);
  174. static const struct i2c_device_id al3010_id[] = {
  175. {"al3010", },
  176. {}
  177. };
  178. MODULE_DEVICE_TABLE(i2c, al3010_id);
  179. static const struct of_device_id al3010_of_match[] = {
  180. { .compatible = "dynaimage,al3010", },
  181. {},
  182. };
  183. MODULE_DEVICE_TABLE(of, al3010_of_match);
  184. static struct i2c_driver al3010_driver = {
  185. .driver = {
  186. .name = AL3010_DRV_NAME,
  187. .of_match_table = al3010_of_match,
  188. .pm = pm_sleep_ptr(&al3010_pm_ops),
  189. },
  190. .probe = al3010_probe,
  191. .id_table = al3010_id,
  192. };
  193. module_i2c_driver(al3010_driver);
  194. MODULE_AUTHOR("Daniel Baluta <[email protected]>");
  195. MODULE_AUTHOR("David Heidelberg <[email protected]>");
  196. MODULE_DESCRIPTION("AL3010 Ambient Light Sensor driver");
  197. MODULE_LICENSE("GPL v2");