al3320a.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AL3320A - Dyna Image Ambient Light Sensor
  4. *
  5. * Copyright (c) 2014, Intel Corporation.
  6. *
  7. * IIO driver for AL3320A (7-bit I2C slave address 0x1C).
  8. *
  9. * TODO: interrupt support, thresholds
  10. * When the driver will get support for interrupt handling, then interrupt
  11. * will need to be disabled before turning sensor OFF in order to avoid
  12. * potential races with the interrupt handling.
  13. */
  14. #include <linux/bitfield.h>
  15. #include <linux/i2c.h>
  16. #include <linux/module.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #define AL3320A_DRV_NAME "al3320a"
  21. #define AL3320A_REG_CONFIG 0x00
  22. #define AL3320A_REG_STATUS 0x01
  23. #define AL3320A_REG_INT 0x02
  24. #define AL3320A_REG_WAIT 0x06
  25. #define AL3320A_REG_CONFIG_RANGE 0x07
  26. #define AL3320A_REG_PERSIST 0x08
  27. #define AL3320A_REG_MEAN_TIME 0x09
  28. #define AL3320A_REG_ADUMMY 0x0A
  29. #define AL3320A_REG_DATA_LOW 0x22
  30. #define AL3320A_REG_LOW_THRESH_LOW 0x30
  31. #define AL3320A_REG_LOW_THRESH_HIGH 0x31
  32. #define AL3320A_REG_HIGH_THRESH_LOW 0x32
  33. #define AL3320A_REG_HIGH_THRESH_HIGH 0x33
  34. #define AL3320A_CONFIG_DISABLE 0x00
  35. #define AL3320A_CONFIG_ENABLE 0x01
  36. #define AL3320A_GAIN_MASK GENMASK(2, 1)
  37. /* chip params default values */
  38. #define AL3320A_DEFAULT_MEAN_TIME 4
  39. #define AL3320A_DEFAULT_WAIT_TIME 0 /* no waiting */
  40. #define AL3320A_SCALE_AVAILABLE "0.512 0.128 0.032 0.01"
  41. enum al3320a_range {
  42. AL3320A_RANGE_1, /* 33.28 Klx */
  43. AL3320A_RANGE_2, /* 8.32 Klx */
  44. AL3320A_RANGE_3, /* 2.08 Klx */
  45. AL3320A_RANGE_4 /* 0.65 Klx */
  46. };
  47. static const int al3320a_scales[][2] = {
  48. {0, 512000}, {0, 128000}, {0, 32000}, {0, 10000}
  49. };
  50. struct al3320a_data {
  51. struct i2c_client *client;
  52. };
  53. static const struct iio_chan_spec al3320a_channels[] = {
  54. {
  55. .type = IIO_LIGHT,
  56. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  57. BIT(IIO_CHAN_INFO_SCALE),
  58. }
  59. };
  60. static IIO_CONST_ATTR(in_illuminance_scale_available, AL3320A_SCALE_AVAILABLE);
  61. static struct attribute *al3320a_attributes[] = {
  62. &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
  63. NULL,
  64. };
  65. static const struct attribute_group al3320a_attribute_group = {
  66. .attrs = al3320a_attributes,
  67. };
  68. static int al3320a_set_pwr(struct i2c_client *client, bool pwr)
  69. {
  70. u8 val = pwr ? AL3320A_CONFIG_ENABLE : AL3320A_CONFIG_DISABLE;
  71. return i2c_smbus_write_byte_data(client, AL3320A_REG_CONFIG, val);
  72. }
  73. static void al3320a_set_pwr_off(void *_data)
  74. {
  75. struct al3320a_data *data = _data;
  76. al3320a_set_pwr(data->client, false);
  77. }
  78. static int al3320a_init(struct al3320a_data *data)
  79. {
  80. int ret;
  81. ret = al3320a_set_pwr(data->client, true);
  82. if (ret < 0)
  83. return ret;
  84. ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_CONFIG_RANGE,
  85. FIELD_PREP(AL3320A_GAIN_MASK,
  86. AL3320A_RANGE_3));
  87. if (ret < 0)
  88. return ret;
  89. ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_MEAN_TIME,
  90. AL3320A_DEFAULT_MEAN_TIME);
  91. if (ret < 0)
  92. return ret;
  93. ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_WAIT,
  94. AL3320A_DEFAULT_WAIT_TIME);
  95. if (ret < 0)
  96. return ret;
  97. return 0;
  98. }
  99. static int al3320a_read_raw(struct iio_dev *indio_dev,
  100. struct iio_chan_spec const *chan, int *val,
  101. int *val2, long mask)
  102. {
  103. struct al3320a_data *data = iio_priv(indio_dev);
  104. int ret;
  105. switch (mask) {
  106. case IIO_CHAN_INFO_RAW:
  107. /*
  108. * ALS ADC value is stored in two adjacent registers:
  109. * - low byte of output is stored at AL3320A_REG_DATA_LOW
  110. * - high byte of output is stored at AL3320A_REG_DATA_LOW + 1
  111. */
  112. ret = i2c_smbus_read_word_data(data->client,
  113. AL3320A_REG_DATA_LOW);
  114. if (ret < 0)
  115. return ret;
  116. *val = ret;
  117. return IIO_VAL_INT;
  118. case IIO_CHAN_INFO_SCALE:
  119. ret = i2c_smbus_read_byte_data(data->client,
  120. AL3320A_REG_CONFIG_RANGE);
  121. if (ret < 0)
  122. return ret;
  123. ret = FIELD_GET(AL3320A_GAIN_MASK, ret);
  124. *val = al3320a_scales[ret][0];
  125. *val2 = al3320a_scales[ret][1];
  126. return IIO_VAL_INT_PLUS_MICRO;
  127. }
  128. return -EINVAL;
  129. }
  130. static int al3320a_write_raw(struct iio_dev *indio_dev,
  131. struct iio_chan_spec const *chan, int val,
  132. int val2, long mask)
  133. {
  134. struct al3320a_data *data = iio_priv(indio_dev);
  135. int i;
  136. switch (mask) {
  137. case IIO_CHAN_INFO_SCALE:
  138. for (i = 0; i < ARRAY_SIZE(al3320a_scales); i++) {
  139. if (val != al3320a_scales[i][0] ||
  140. val2 != al3320a_scales[i][1])
  141. continue;
  142. return i2c_smbus_write_byte_data(data->client,
  143. AL3320A_REG_CONFIG_RANGE,
  144. FIELD_PREP(AL3320A_GAIN_MASK, i));
  145. }
  146. break;
  147. }
  148. return -EINVAL;
  149. }
  150. static const struct iio_info al3320a_info = {
  151. .read_raw = al3320a_read_raw,
  152. .write_raw = al3320a_write_raw,
  153. .attrs = &al3320a_attribute_group,
  154. };
  155. static int al3320a_probe(struct i2c_client *client)
  156. {
  157. struct al3320a_data *data;
  158. struct iio_dev *indio_dev;
  159. int ret;
  160. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  161. if (!indio_dev)
  162. return -ENOMEM;
  163. data = iio_priv(indio_dev);
  164. i2c_set_clientdata(client, indio_dev);
  165. data->client = client;
  166. indio_dev->info = &al3320a_info;
  167. indio_dev->name = AL3320A_DRV_NAME;
  168. indio_dev->channels = al3320a_channels;
  169. indio_dev->num_channels = ARRAY_SIZE(al3320a_channels);
  170. indio_dev->modes = INDIO_DIRECT_MODE;
  171. ret = al3320a_init(data);
  172. if (ret < 0) {
  173. dev_err(&client->dev, "al3320a chip init failed\n");
  174. return ret;
  175. }
  176. ret = devm_add_action_or_reset(&client->dev,
  177. al3320a_set_pwr_off,
  178. data);
  179. if (ret < 0)
  180. return ret;
  181. return devm_iio_device_register(&client->dev, indio_dev);
  182. }
  183. static int al3320a_suspend(struct device *dev)
  184. {
  185. return al3320a_set_pwr(to_i2c_client(dev), false);
  186. }
  187. static int al3320a_resume(struct device *dev)
  188. {
  189. return al3320a_set_pwr(to_i2c_client(dev), true);
  190. }
  191. static DEFINE_SIMPLE_DEV_PM_OPS(al3320a_pm_ops, al3320a_suspend,
  192. al3320a_resume);
  193. static const struct i2c_device_id al3320a_id[] = {
  194. { "al3320a" },
  195. {}
  196. };
  197. MODULE_DEVICE_TABLE(i2c, al3320a_id);
  198. static const struct of_device_id al3320a_of_match[] = {
  199. { .compatible = "dynaimage,al3320a", },
  200. {},
  201. };
  202. MODULE_DEVICE_TABLE(of, al3320a_of_match);
  203. static const struct acpi_device_id al3320a_acpi_match[] = {
  204. {"CALS0001"},
  205. { },
  206. };
  207. MODULE_DEVICE_TABLE(acpi, al3320a_acpi_match);
  208. static struct i2c_driver al3320a_driver = {
  209. .driver = {
  210. .name = AL3320A_DRV_NAME,
  211. .of_match_table = al3320a_of_match,
  212. .pm = pm_sleep_ptr(&al3320a_pm_ops),
  213. .acpi_match_table = al3320a_acpi_match,
  214. },
  215. .probe = al3320a_probe,
  216. .id_table = al3320a_id,
  217. };
  218. module_i2c_driver(al3320a_driver);
  219. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  220. MODULE_DESCRIPTION("AL3320A Ambient Light Sensor driver");
  221. MODULE_LICENSE("GPL v2");