apds9802als.c 7.6 KB

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