isl29020.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * isl29020.c - Intersil ALS Driver
  3. *
  4. * Copyright (C) 2008 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. * Data sheet at: http://www.intersil.com/data/fn/fn6505.pdf
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c.h>
  28. #include <linux/err.h>
  29. #include <linux/delay.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/pm_runtime.h>
  32. static DEFINE_MUTEX(mutex);
  33. static ssize_t als_sensing_range_show(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct i2c_client *client = to_i2c_client(dev);
  37. int val;
  38. val = i2c_smbus_read_byte_data(client, 0x00);
  39. if (val < 0)
  40. return val;
  41. return sprintf(buf, "%d000\n", 1 << (2 * (val & 3)));
  42. }
  43. static ssize_t als_lux_input_data_show(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct i2c_client *client = to_i2c_client(dev);
  47. int ret_val, val;
  48. unsigned long int lux;
  49. int temp;
  50. pm_runtime_get_sync(dev);
  51. msleep(100);
  52. mutex_lock(&mutex);
  53. temp = i2c_smbus_read_byte_data(client, 0x02); /* MSB data */
  54. if (temp < 0) {
  55. pm_runtime_put_sync(dev);
  56. mutex_unlock(&mutex);
  57. return temp;
  58. }
  59. ret_val = i2c_smbus_read_byte_data(client, 0x01); /* LSB data */
  60. mutex_unlock(&mutex);
  61. if (ret_val < 0) {
  62. pm_runtime_put_sync(dev);
  63. return ret_val;
  64. }
  65. ret_val |= temp << 8;
  66. val = i2c_smbus_read_byte_data(client, 0x00);
  67. pm_runtime_put_sync(dev);
  68. if (val < 0)
  69. return val;
  70. lux = ((((1 << (2 * (val & 3))))*1000) * ret_val) / 65536;
  71. return sprintf(buf, "%ld\n", lux);
  72. }
  73. static ssize_t als_sensing_range_store(struct device *dev,
  74. struct device_attribute *attr, const char *buf, size_t count)
  75. {
  76. struct i2c_client *client = to_i2c_client(dev);
  77. int ret_val;
  78. unsigned long val;
  79. if (strict_strtoul(buf, 10, &val))
  80. return -EINVAL;
  81. if (val < 1 || val > 64000)
  82. return -EINVAL;
  83. /* Pick the smallest sensor range that will meet our requirements */
  84. if (val <= 1000)
  85. val = 1;
  86. else if (val <= 4000)
  87. val = 2;
  88. else if (val <= 16000)
  89. val = 3;
  90. else
  91. val = 4;
  92. ret_val = i2c_smbus_read_byte_data(client, 0x00);
  93. if (ret_val < 0)
  94. return ret_val;
  95. ret_val &= 0xFC; /*reset the bit before setting them */
  96. ret_val |= val - 1;
  97. ret_val = i2c_smbus_write_byte_data(client, 0x00, ret_val);
  98. if (ret_val < 0)
  99. return ret_val;
  100. return count;
  101. }
  102. static void als_set_power_state(struct i2c_client *client, int enable)
  103. {
  104. int ret_val;
  105. ret_val = i2c_smbus_read_byte_data(client, 0x00);
  106. if (ret_val < 0)
  107. return;
  108. if (enable)
  109. ret_val |= 0x80;
  110. else
  111. ret_val &= 0x7F;
  112. i2c_smbus_write_byte_data(client, 0x00, ret_val);
  113. }
  114. static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
  115. als_sensing_range_show, als_sensing_range_store);
  116. static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux_input_data_show, NULL);
  117. static struct attribute *mid_att_als[] = {
  118. &dev_attr_lux0_sensor_range.attr,
  119. &dev_attr_lux0_input.attr,
  120. NULL
  121. };
  122. static struct attribute_group m_als_gr = {
  123. .name = "isl29020",
  124. .attrs = mid_att_als
  125. };
  126. static int als_set_default_config(struct i2c_client *client)
  127. {
  128. int retval;
  129. retval = i2c_smbus_write_byte_data(client, 0x00, 0xc0);
  130. if (retval < 0) {
  131. dev_err(&client->dev, "default write failed.");
  132. return retval;
  133. }
  134. return 0;;
  135. }
  136. static int isl29020_probe(struct i2c_client *client,
  137. const struct i2c_device_id *id)
  138. {
  139. int res;
  140. res = als_set_default_config(client);
  141. if (res < 0)
  142. return res;
  143. res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
  144. if (res) {
  145. dev_err(&client->dev, "isl29020: device create file failed\n");
  146. return res;
  147. }
  148. dev_info(&client->dev, "%s isl29020: ALS chip found\n", client->name);
  149. als_set_power_state(client, 0);
  150. pm_runtime_enable(&client->dev);
  151. return res;
  152. }
  153. static int isl29020_remove(struct i2c_client *client)
  154. {
  155. sysfs_remove_group(&client->dev.kobj, &m_als_gr);
  156. return 0;
  157. }
  158. static struct i2c_device_id isl29020_id[] = {
  159. { "isl29020", 0 },
  160. { }
  161. };
  162. MODULE_DEVICE_TABLE(i2c, isl29020_id);
  163. #ifdef CONFIG_PM
  164. static int isl29020_runtime_suspend(struct device *dev)
  165. {
  166. struct i2c_client *client = to_i2c_client(dev);
  167. als_set_power_state(client, 0);
  168. return 0;
  169. }
  170. static int isl29020_runtime_resume(struct device *dev)
  171. {
  172. struct i2c_client *client = to_i2c_client(dev);
  173. als_set_power_state(client, 1);
  174. return 0;
  175. }
  176. static const struct dev_pm_ops isl29020_pm_ops = {
  177. .runtime_suspend = isl29020_runtime_suspend,
  178. .runtime_resume = isl29020_runtime_resume,
  179. };
  180. #define ISL29020_PM_OPS (&isl29020_pm_ops)
  181. #else /* CONFIG_PM */
  182. #define ISL29020_PM_OPS NULL
  183. #endif /* CONFIG_PM */
  184. static struct i2c_driver isl29020_driver = {
  185. .driver = {
  186. .name = "isl29020",
  187. .pm = ISL29020_PM_OPS,
  188. },
  189. .probe = isl29020_probe,
  190. .remove = isl29020_remove,
  191. .id_table = isl29020_id,
  192. };
  193. static int __init sensor_isl29020_init(void)
  194. {
  195. return i2c_add_driver(&isl29020_driver);
  196. }
  197. static void __exit sensor_isl29020_exit(void)
  198. {
  199. i2c_del_driver(&isl29020_driver);
  200. }
  201. module_init(sensor_isl29020_init);
  202. module_exit(sensor_isl29020_exit);
  203. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com>");
  204. MODULE_DESCRIPTION("Intersil isl29020 ALS Driver");
  205. MODULE_LICENSE("GPL v2");