apds9802als.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * apds9802als.c - apds9802 ALS Driver
  4. *
  5. * Copyright (C) 2009 Intel Corp
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/mutex.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/pm_runtime.h>
  19. #define ALS_MIN_RANGE_VAL 1
  20. #define ALS_MAX_RANGE_VAL 2
  21. #define POWER_STA_ENABLE 1
  22. #define POWER_STA_DISABLE 0
  23. #define DRIVER_NAME "apds9802als"
  24. struct als_data {
  25. struct mutex mutex;
  26. };
  27. static ssize_t als_sensing_range_show(struct device *dev,
  28. struct device_attribute *attr, char *buf)
  29. {
  30. struct i2c_client *client = to_i2c_client(dev);
  31. int val;
  32. val = i2c_smbus_read_byte_data(client, 0x81);
  33. if (val < 0)
  34. return val;
  35. if (val & 1)
  36. return sprintf(buf, "4095\n");
  37. else
  38. return sprintf(buf, "65535\n");
  39. }
  40. static int als_wait_for_data_ready(struct device *dev)
  41. {
  42. struct i2c_client *client = to_i2c_client(dev);
  43. int ret;
  44. int retry = 10;
  45. do {
  46. msleep(30);
  47. ret = i2c_smbus_read_byte_data(client, 0x86);
  48. } while (!(ret & 0x80) && retry--);
  49. if (retry < 0) {
  50. dev_warn(dev, "timeout waiting for data ready\n");
  51. return -ETIMEDOUT;
  52. }
  53. return 0;
  54. }
  55. static ssize_t als_lux0_input_data_show(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct i2c_client *client = to_i2c_client(dev);
  59. struct als_data *data = i2c_get_clientdata(client);
  60. int ret_val;
  61. int temp;
  62. /* Protect against parallel reads */
  63. pm_runtime_get_sync(dev);
  64. mutex_lock(&data->mutex);
  65. /* clear EOC interrupt status */
  66. i2c_smbus_write_byte(client, 0x40);
  67. /* start measurement */
  68. temp = i2c_smbus_read_byte_data(client, 0x81);
  69. i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
  70. ret_val = als_wait_for_data_ready(dev);
  71. if (ret_val < 0)
  72. goto failed;
  73. temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
  74. if (temp < 0) {
  75. ret_val = temp;
  76. goto failed;
  77. }
  78. ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
  79. if (ret_val < 0)
  80. goto failed;
  81. mutex_unlock(&data->mutex);
  82. pm_runtime_put_sync(dev);
  83. temp = (ret_val << 8) | temp;
  84. return sprintf(buf, "%d\n", temp);
  85. failed:
  86. mutex_unlock(&data->mutex);
  87. pm_runtime_put_sync(dev);
  88. return ret_val;
  89. }
  90. static ssize_t als_sensing_range_store(struct device *dev,
  91. struct device_attribute *attr, const char *buf, size_t count)
  92. {
  93. struct i2c_client *client = to_i2c_client(dev);
  94. struct als_data *data = i2c_get_clientdata(client);
  95. int ret_val;
  96. unsigned long val;
  97. ret_val = kstrtoul(buf, 10, &val);
  98. if (ret_val)
  99. return ret_val;
  100. if (val < 4096)
  101. val = 1;
  102. else if (val < 65536)
  103. val = 2;
  104. else
  105. return -ERANGE;
  106. pm_runtime_get_sync(dev);
  107. /* Make sure nobody else reads/modifies/writes 0x81 while we
  108. are active */
  109. mutex_lock(&data->mutex);
  110. ret_val = i2c_smbus_read_byte_data(client, 0x81);
  111. if (ret_val < 0)
  112. goto fail;
  113. /* Reset the bits before setting them */
  114. ret_val = ret_val & 0xFA;
  115. if (val == 1) /* Setting detection range up to 4k LUX */
  116. ret_val = (ret_val | 0x01);
  117. else /* Setting detection range up to 64k LUX*/
  118. ret_val = (ret_val | 0x00);
  119. ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
  120. if (ret_val >= 0) {
  121. /* All OK */
  122. mutex_unlock(&data->mutex);
  123. pm_runtime_put_sync(dev);
  124. return count;
  125. }
  126. fail:
  127. mutex_unlock(&data->mutex);
  128. pm_runtime_put_sync(dev);
  129. return ret_val;
  130. }
  131. static int als_set_power_state(struct i2c_client *client, bool on_off)
  132. {
  133. int ret_val;
  134. struct als_data *data = i2c_get_clientdata(client);
  135. mutex_lock(&data->mutex);
  136. ret_val = i2c_smbus_read_byte_data(client, 0x80);
  137. if (ret_val < 0)
  138. goto fail;
  139. if (on_off)
  140. ret_val = ret_val | 0x01;
  141. else
  142. ret_val = ret_val & 0xFE;
  143. ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val);
  144. fail:
  145. mutex_unlock(&data->mutex);
  146. return ret_val;
  147. }
  148. static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
  149. als_sensing_range_show, als_sensing_range_store);
  150. static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
  151. static struct attribute *mid_att_als[] = {
  152. &dev_attr_lux0_sensor_range.attr,
  153. &dev_attr_lux0_input.attr,
  154. NULL
  155. };
  156. static const struct attribute_group m_als_gr = {
  157. .name = "apds9802als",
  158. .attrs = mid_att_als
  159. };
  160. static int als_set_default_config(struct i2c_client *client)
  161. {
  162. int ret_val;
  163. /* Write the command and then switch on */
  164. ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01);
  165. if (ret_val < 0) {
  166. dev_err(&client->dev, "failed default switch on write\n");
  167. return ret_val;
  168. }
  169. /* detection range: 1~64K Lux, maunal measurement */
  170. ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
  171. if (ret_val < 0)
  172. dev_err(&client->dev, "failed default LUX on write\n");
  173. /* We always get 0 for the 1st measurement after system power on,
  174. * so make sure it is finished before user asks for data.
  175. */
  176. als_wait_for_data_ready(&client->dev);
  177. return ret_val;
  178. }
  179. static int apds9802als_probe(struct i2c_client *client,
  180. const struct i2c_device_id *id)
  181. {
  182. int res;
  183. struct als_data *data;
  184. data = kzalloc(sizeof(struct als_data), GFP_KERNEL);
  185. if (data == NULL) {
  186. dev_err(&client->dev, "Memory allocation failed\n");
  187. return -ENOMEM;
  188. }
  189. i2c_set_clientdata(client, data);
  190. res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
  191. if (res) {
  192. dev_err(&client->dev, "device create file failed\n");
  193. goto als_error1;
  194. }
  195. dev_info(&client->dev, "ALS chip found\n");
  196. als_set_default_config(client);
  197. mutex_init(&data->mutex);
  198. pm_runtime_set_active(&client->dev);
  199. pm_runtime_enable(&client->dev);
  200. return res;
  201. als_error1:
  202. kfree(data);
  203. return res;
  204. }
  205. static int apds9802als_remove(struct i2c_client *client)
  206. {
  207. struct als_data *data = i2c_get_clientdata(client);
  208. pm_runtime_get_sync(&client->dev);
  209. als_set_power_state(client, false);
  210. sysfs_remove_group(&client->dev.kobj, &m_als_gr);
  211. pm_runtime_disable(&client->dev);
  212. pm_runtime_set_suspended(&client->dev);
  213. pm_runtime_put_noidle(&client->dev);
  214. kfree(data);
  215. return 0;
  216. }
  217. #ifdef CONFIG_PM
  218. static int apds9802als_suspend(struct device *dev)
  219. {
  220. struct i2c_client *client = to_i2c_client(dev);
  221. als_set_power_state(client, false);
  222. return 0;
  223. }
  224. static int apds9802als_resume(struct device *dev)
  225. {
  226. struct i2c_client *client = to_i2c_client(dev);
  227. als_set_power_state(client, true);
  228. return 0;
  229. }
  230. static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops, apds9802als_suspend,
  231. apds9802als_resume, NULL);
  232. #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
  233. #else /* CONFIG_PM */
  234. #define APDS9802ALS_PM_OPS NULL
  235. #endif /* CONFIG_PM */
  236. static const struct i2c_device_id apds9802als_id[] = {
  237. { DRIVER_NAME, 0 },
  238. { }
  239. };
  240. MODULE_DEVICE_TABLE(i2c, apds9802als_id);
  241. static struct i2c_driver apds9802als_driver = {
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. .pm = APDS9802ALS_PM_OPS,
  245. },
  246. .probe = apds9802als_probe,
  247. .remove = apds9802als_remove,
  248. .id_table = apds9802als_id,
  249. };
  250. module_i2c_driver(apds9802als_driver);
  251. MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
  252. MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
  253. MODULE_LICENSE("GPL v2");