isl29020.c 5.0 KB

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