isl29020.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. ret_val = kstrtoul(buf, 10, &val);
  80. if (ret_val)
  81. return ret_val;
  82. if (val < 1 || val > 64000)
  83. return -EINVAL;
  84. /* Pick the smallest sensor range that will meet our requirements */
  85. if (val <= 1000)
  86. val = 1;
  87. else if (val <= 4000)
  88. val = 2;
  89. else if (val <= 16000)
  90. val = 3;
  91. else
  92. val = 4;
  93. ret_val = i2c_smbus_read_byte_data(client, 0x00);
  94. if (ret_val < 0)
  95. return ret_val;
  96. ret_val &= 0xFC; /*reset the bit before setting them */
  97. ret_val |= val - 1;
  98. ret_val = i2c_smbus_write_byte_data(client, 0x00, ret_val);
  99. if (ret_val < 0)
  100. return ret_val;
  101. return count;
  102. }
  103. static void als_set_power_state(struct i2c_client *client, int enable)
  104. {
  105. int ret_val;
  106. ret_val = i2c_smbus_read_byte_data(client, 0x00);
  107. if (ret_val < 0)
  108. return;
  109. if (enable)
  110. ret_val |= 0x80;
  111. else
  112. ret_val &= 0x7F;
  113. i2c_smbus_write_byte_data(client, 0x00, ret_val);
  114. }
  115. static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
  116. als_sensing_range_show, als_sensing_range_store);
  117. static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux_input_data_show, NULL);
  118. static struct attribute *mid_att_als[] = {
  119. &dev_attr_lux0_sensor_range.attr,
  120. &dev_attr_lux0_input.attr,
  121. NULL
  122. };
  123. static struct attribute_group m_als_gr = {
  124. .name = "isl29020",
  125. .attrs = mid_att_als
  126. };
  127. static int als_set_default_config(struct i2c_client *client)
  128. {
  129. int retval;
  130. retval = i2c_smbus_write_byte_data(client, 0x00, 0xc0);
  131. if (retval < 0) {
  132. dev_err(&client->dev, "default write failed.");
  133. return retval;
  134. }
  135. return 0;
  136. }
  137. static int isl29020_probe(struct i2c_client *client,
  138. const struct i2c_device_id *id)
  139. {
  140. int res;
  141. res = als_set_default_config(client);
  142. if (res < 0)
  143. return res;
  144. res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
  145. if (res) {
  146. dev_err(&client->dev, "isl29020: device create file failed\n");
  147. return res;
  148. }
  149. dev_info(&client->dev, "%s isl29020: ALS chip found\n", client->name);
  150. als_set_power_state(client, 0);
  151. pm_runtime_enable(&client->dev);
  152. return res;
  153. }
  154. static int isl29020_remove(struct i2c_client *client)
  155. {
  156. sysfs_remove_group(&client->dev.kobj, &m_als_gr);
  157. return 0;
  158. }
  159. static struct i2c_device_id isl29020_id[] = {
  160. { "isl29020", 0 },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(i2c, isl29020_id);
  164. #ifdef CONFIG_PM
  165. static int isl29020_runtime_suspend(struct device *dev)
  166. {
  167. struct i2c_client *client = to_i2c_client(dev);
  168. als_set_power_state(client, 0);
  169. return 0;
  170. }
  171. static int isl29020_runtime_resume(struct device *dev)
  172. {
  173. struct i2c_client *client = to_i2c_client(dev);
  174. als_set_power_state(client, 1);
  175. return 0;
  176. }
  177. static const struct dev_pm_ops isl29020_pm_ops = {
  178. .runtime_suspend = isl29020_runtime_suspend,
  179. .runtime_resume = isl29020_runtime_resume,
  180. };
  181. #define ISL29020_PM_OPS (&isl29020_pm_ops)
  182. #else /* CONFIG_PM */
  183. #define ISL29020_PM_OPS NULL
  184. #endif /* CONFIG_PM */
  185. static struct i2c_driver isl29020_driver = {
  186. .driver = {
  187. .name = "isl29020",
  188. .pm = ISL29020_PM_OPS,
  189. },
  190. .probe = isl29020_probe,
  191. .remove = isl29020_remove,
  192. .id_table = isl29020_id,
  193. };
  194. module_i2c_driver(isl29020_driver);
  195. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com>");
  196. MODULE_DESCRIPTION("Intersil isl29020 ALS Driver");
  197. MODULE_LICENSE("GPL v2");