tsl4531.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tsl4531.c - Support for TAOS TSL4531 ambient light sensor
  4. *
  5. * Copyright 2013 Peter Meerwald <[email protected]>
  6. *
  7. * IIO driver for the TSL4531x family
  8. * TSL45311/TSL45313: 7-bit I2C slave address 0x39
  9. * TSL45315/TSL45317: 7-bit I2C slave address 0x29
  10. *
  11. * TODO: single cycle measurement
  12. */
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/err.h>
  16. #include <linux/delay.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #define TSL4531_DRV_NAME "tsl4531"
  20. #define TSL4531_COMMAND BIT(7)
  21. #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00)
  22. #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01)
  23. #define TSL4531_DATA (TSL4531_COMMAND | 0x04)
  24. #define TSL4531_ID (TSL4531_COMMAND | 0x0a)
  25. /* operating modes in control register */
  26. #define TSL4531_MODE_POWERDOWN 0x00
  27. #define TSL4531_MODE_SINGLE_ADC 0x02
  28. #define TSL4531_MODE_NORMAL 0x03
  29. /* integration time control in config register */
  30. #define TSL4531_TCNTRL_400MS 0x00
  31. #define TSL4531_TCNTRL_200MS 0x01
  32. #define TSL4531_TCNTRL_100MS 0x02
  33. /* part number in id register */
  34. #define TSL45311_ID 0x8
  35. #define TSL45313_ID 0x9
  36. #define TSL45315_ID 0xa
  37. #define TSL45317_ID 0xb
  38. #define TSL4531_ID_SHIFT 4
  39. struct tsl4531_data {
  40. struct i2c_client *client;
  41. struct mutex lock;
  42. int int_time;
  43. };
  44. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4");
  45. static struct attribute *tsl4531_attributes[] = {
  46. &iio_const_attr_integration_time_available.dev_attr.attr,
  47. NULL
  48. };
  49. static const struct attribute_group tsl4531_attribute_group = {
  50. .attrs = tsl4531_attributes,
  51. };
  52. static const struct iio_chan_spec tsl4531_channels[] = {
  53. {
  54. .type = IIO_LIGHT,
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  56. BIT(IIO_CHAN_INFO_SCALE) |
  57. BIT(IIO_CHAN_INFO_INT_TIME)
  58. }
  59. };
  60. static int tsl4531_read_raw(struct iio_dev *indio_dev,
  61. struct iio_chan_spec const *chan,
  62. int *val, int *val2, long mask)
  63. {
  64. struct tsl4531_data *data = iio_priv(indio_dev);
  65. int ret;
  66. switch (mask) {
  67. case IIO_CHAN_INFO_RAW:
  68. ret = i2c_smbus_read_word_data(data->client,
  69. TSL4531_DATA);
  70. if (ret < 0)
  71. return ret;
  72. *val = ret;
  73. return IIO_VAL_INT;
  74. case IIO_CHAN_INFO_SCALE:
  75. /* 0.. 1x, 1 .. 2x, 2 .. 4x */
  76. *val = 1 << data->int_time;
  77. return IIO_VAL_INT;
  78. case IIO_CHAN_INFO_INT_TIME:
  79. if (data->int_time == 0)
  80. *val2 = 400000;
  81. else if (data->int_time == 1)
  82. *val2 = 200000;
  83. else if (data->int_time == 2)
  84. *val2 = 100000;
  85. else
  86. return -EINVAL;
  87. *val = 0;
  88. return IIO_VAL_INT_PLUS_MICRO;
  89. default:
  90. return -EINVAL;
  91. }
  92. }
  93. static int tsl4531_write_raw(struct iio_dev *indio_dev,
  94. struct iio_chan_spec const *chan,
  95. int val, int val2, long mask)
  96. {
  97. struct tsl4531_data *data = iio_priv(indio_dev);
  98. int int_time, ret;
  99. switch (mask) {
  100. case IIO_CHAN_INFO_INT_TIME:
  101. if (val != 0)
  102. return -EINVAL;
  103. if (val2 == 400000)
  104. int_time = 0;
  105. else if (val2 == 200000)
  106. int_time = 1;
  107. else if (val2 == 100000)
  108. int_time = 2;
  109. else
  110. return -EINVAL;
  111. mutex_lock(&data->lock);
  112. ret = i2c_smbus_write_byte_data(data->client,
  113. TSL4531_CONFIG, int_time);
  114. if (ret >= 0)
  115. data->int_time = int_time;
  116. mutex_unlock(&data->lock);
  117. return ret;
  118. default:
  119. return -EINVAL;
  120. }
  121. }
  122. static const struct iio_info tsl4531_info = {
  123. .read_raw = tsl4531_read_raw,
  124. .write_raw = tsl4531_write_raw,
  125. .attrs = &tsl4531_attribute_group,
  126. };
  127. static int tsl4531_check_id(struct i2c_client *client)
  128. {
  129. int ret = i2c_smbus_read_byte_data(client, TSL4531_ID);
  130. if (ret < 0)
  131. return ret;
  132. switch (ret >> TSL4531_ID_SHIFT) {
  133. case TSL45311_ID:
  134. case TSL45313_ID:
  135. case TSL45315_ID:
  136. case TSL45317_ID:
  137. return 0;
  138. default:
  139. return -ENODEV;
  140. }
  141. }
  142. static int tsl4531_probe(struct i2c_client *client,
  143. const struct i2c_device_id *id)
  144. {
  145. struct tsl4531_data *data;
  146. struct iio_dev *indio_dev;
  147. int ret;
  148. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  149. if (!indio_dev)
  150. return -ENOMEM;
  151. data = iio_priv(indio_dev);
  152. i2c_set_clientdata(client, indio_dev);
  153. data->client = client;
  154. mutex_init(&data->lock);
  155. ret = tsl4531_check_id(client);
  156. if (ret) {
  157. dev_err(&client->dev, "no TSL4531 sensor\n");
  158. return ret;
  159. }
  160. ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONTROL,
  161. TSL4531_MODE_NORMAL);
  162. if (ret < 0)
  163. return ret;
  164. ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONFIG,
  165. TSL4531_TCNTRL_400MS);
  166. if (ret < 0)
  167. return ret;
  168. indio_dev->info = &tsl4531_info;
  169. indio_dev->channels = tsl4531_channels;
  170. indio_dev->num_channels = ARRAY_SIZE(tsl4531_channels);
  171. indio_dev->name = TSL4531_DRV_NAME;
  172. indio_dev->modes = INDIO_DIRECT_MODE;
  173. return iio_device_register(indio_dev);
  174. }
  175. static int tsl4531_powerdown(struct i2c_client *client)
  176. {
  177. return i2c_smbus_write_byte_data(client, TSL4531_CONTROL,
  178. TSL4531_MODE_POWERDOWN);
  179. }
  180. static void tsl4531_remove(struct i2c_client *client)
  181. {
  182. iio_device_unregister(i2c_get_clientdata(client));
  183. tsl4531_powerdown(client);
  184. }
  185. static int tsl4531_suspend(struct device *dev)
  186. {
  187. return tsl4531_powerdown(to_i2c_client(dev));
  188. }
  189. static int tsl4531_resume(struct device *dev)
  190. {
  191. return i2c_smbus_write_byte_data(to_i2c_client(dev), TSL4531_CONTROL,
  192. TSL4531_MODE_NORMAL);
  193. }
  194. static DEFINE_SIMPLE_DEV_PM_OPS(tsl4531_pm_ops, tsl4531_suspend,
  195. tsl4531_resume);
  196. static const struct i2c_device_id tsl4531_id[] = {
  197. { "tsl4531", 0 },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE(i2c, tsl4531_id);
  201. static struct i2c_driver tsl4531_driver = {
  202. .driver = {
  203. .name = TSL4531_DRV_NAME,
  204. .pm = pm_sleep_ptr(&tsl4531_pm_ops),
  205. },
  206. .probe = tsl4531_probe,
  207. .remove = tsl4531_remove,
  208. .id_table = tsl4531_id,
  209. };
  210. module_i2c_driver(tsl4531_driver);
  211. MODULE_AUTHOR("Peter Meerwald <[email protected]>");
  212. MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver");
  213. MODULE_LICENSE("GPL");