isl29020.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/err.h>
  28. #include <linux/delay.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/pm_runtime.h>
  31. static DEFINE_MUTEX(mutex);
  32. static ssize_t als_sensing_range_show(struct device *dev,
  33. struct device_attribute *attr, char *buf)
  34. {
  35. struct i2c_client *client = to_i2c_client(dev);
  36. int val;
  37. val = i2c_smbus_read_byte_data(client, 0x00);
  38. if (val < 0)
  39. return val;
  40. return sprintf(buf, "%d000\n", 1 << (2 * (val & 3)));
  41. }
  42. static ssize_t als_lux_input_data_show(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct i2c_client *client = to_i2c_client(dev);
  46. int ret_val, val;
  47. unsigned long int lux;
  48. int temp;
  49. pm_runtime_get_sync(dev);
  50. msleep(100);
  51. mutex_lock(&mutex);
  52. temp = i2c_smbus_read_byte_data(client, 0x02); /* MSB data */
  53. if (temp < 0) {
  54. pm_runtime_put_sync(dev);
  55. mutex_unlock(&mutex);
  56. return temp;
  57. }
  58. ret_val = i2c_smbus_read_byte_data(client, 0x01); /* LSB data */
  59. mutex_unlock(&mutex);
  60. if (ret_val < 0) {
  61. pm_runtime_put_sync(dev);
  62. return ret_val;
  63. }
  64. ret_val |= temp << 8;
  65. val = i2c_smbus_read_byte_data(client, 0x00);
  66. pm_runtime_put_sync(dev);
  67. if (val < 0)
  68. return val;
  69. lux = ((((1 << (2 * (val & 3))))*1000) * ret_val) / 65536;
  70. return sprintf(buf, "%ld\n", lux);
  71. }
  72. static ssize_t als_sensing_range_store(struct device *dev,
  73. struct device_attribute *attr, const char *buf, size_t count)
  74. {
  75. struct i2c_client *client = to_i2c_client(dev);
  76. int ret_val;
  77. unsigned long val;
  78. ret_val = kstrtoul(buf, 10, &val);
  79. if (ret_val)
  80. return ret_val;
  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. module_i2c_driver(isl29020_driver);
  194. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com>");
  195. MODULE_DESCRIPTION("Intersil isl29020 ALS Driver");
  196. MODULE_LICENSE("GPL v2");