bh1780gli.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * bh1780gli.c
  3. * ROHM Ambient Light Sensor Driver
  4. *
  5. * Copyright (C) 2010 Texas Instruments
  6. * Author: Hemanth V <hemanthv@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/i2c.h>
  21. #include <linux/slab.h>
  22. #include <linux/mutex.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/delay.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #define BH1780_REG_CONTROL 0x80
  28. #define BH1780_REG_PARTID 0x8A
  29. #define BH1780_REG_MANFID 0x8B
  30. #define BH1780_REG_DLOW 0x8C
  31. #define BH1780_REG_DHIGH 0x8D
  32. #define BH1780_REVMASK (0xf)
  33. #define BH1780_POWMASK (0x3)
  34. #define BH1780_POFF (0x0)
  35. #define BH1780_PON (0x3)
  36. /* power on settling time in ms */
  37. #define BH1780_PON_DELAY 2
  38. struct bh1780_data {
  39. struct i2c_client *client;
  40. int power_state;
  41. /* lock for sysfs operations */
  42. struct mutex lock;
  43. };
  44. static int bh1780_write(struct bh1780_data *ddata, u8 reg, u8 val, char *msg)
  45. {
  46. int ret = i2c_smbus_write_byte_data(ddata->client, reg, val);
  47. if (ret < 0)
  48. dev_err(&ddata->client->dev,
  49. "i2c_smbus_write_byte_data failed error %d Register (%s)\n",
  50. ret, msg);
  51. return ret;
  52. }
  53. static int bh1780_read(struct bh1780_data *ddata, u8 reg, char *msg)
  54. {
  55. int ret = i2c_smbus_read_byte_data(ddata->client, reg);
  56. if (ret < 0)
  57. dev_err(&ddata->client->dev,
  58. "i2c_smbus_read_byte_data failed error %d Register (%s)\n",
  59. ret, msg);
  60. return ret;
  61. }
  62. static ssize_t bh1780_show_lux(struct device *dev,
  63. struct device_attribute *attr, char *buf)
  64. {
  65. struct platform_device *pdev = to_platform_device(dev);
  66. struct bh1780_data *ddata = platform_get_drvdata(pdev);
  67. int lsb, msb;
  68. lsb = bh1780_read(ddata, BH1780_REG_DLOW, "DLOW");
  69. if (lsb < 0)
  70. return lsb;
  71. msb = bh1780_read(ddata, BH1780_REG_DHIGH, "DHIGH");
  72. if (msb < 0)
  73. return msb;
  74. return sprintf(buf, "%d\n", (msb << 8) | lsb);
  75. }
  76. static ssize_t bh1780_show_power_state(struct device *dev,
  77. struct device_attribute *attr,
  78. char *buf)
  79. {
  80. struct platform_device *pdev = to_platform_device(dev);
  81. struct bh1780_data *ddata = platform_get_drvdata(pdev);
  82. int state;
  83. state = bh1780_read(ddata, BH1780_REG_CONTROL, "CONTROL");
  84. if (state < 0)
  85. return state;
  86. return sprintf(buf, "%d\n", state & BH1780_POWMASK);
  87. }
  88. static ssize_t bh1780_store_power_state(struct device *dev,
  89. struct device_attribute *attr,
  90. const char *buf, size_t count)
  91. {
  92. struct platform_device *pdev = to_platform_device(dev);
  93. struct bh1780_data *ddata = platform_get_drvdata(pdev);
  94. unsigned long val;
  95. int error;
  96. error = kstrtoul(buf, 0, &val);
  97. if (error)
  98. return error;
  99. if (val < BH1780_POFF || val > BH1780_PON)
  100. return -EINVAL;
  101. mutex_lock(&ddata->lock);
  102. error = bh1780_write(ddata, BH1780_REG_CONTROL, val, "CONTROL");
  103. if (error < 0) {
  104. mutex_unlock(&ddata->lock);
  105. return error;
  106. }
  107. msleep(BH1780_PON_DELAY);
  108. ddata->power_state = val;
  109. mutex_unlock(&ddata->lock);
  110. return count;
  111. }
  112. static DEVICE_ATTR(lux, S_IRUGO, bh1780_show_lux, NULL);
  113. static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
  114. bh1780_show_power_state, bh1780_store_power_state);
  115. static struct attribute *bh1780_attributes[] = {
  116. &dev_attr_power_state.attr,
  117. &dev_attr_lux.attr,
  118. NULL
  119. };
  120. static const struct attribute_group bh1780_attr_group = {
  121. .attrs = bh1780_attributes,
  122. };
  123. static int bh1780_probe(struct i2c_client *client,
  124. const struct i2c_device_id *id)
  125. {
  126. int ret;
  127. struct bh1780_data *ddata;
  128. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  129. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  130. return -EIO;
  131. ddata = devm_kzalloc(&client->dev, sizeof(struct bh1780_data),
  132. GFP_KERNEL);
  133. if (ddata == NULL)
  134. return -ENOMEM;
  135. ddata->client = client;
  136. i2c_set_clientdata(client, ddata);
  137. ret = bh1780_read(ddata, BH1780_REG_PARTID, "PART ID");
  138. if (ret < 0)
  139. return ret;
  140. dev_info(&client->dev, "Ambient Light Sensor, Rev : %d\n",
  141. (ret & BH1780_REVMASK));
  142. mutex_init(&ddata->lock);
  143. return sysfs_create_group(&client->dev.kobj, &bh1780_attr_group);
  144. }
  145. static int bh1780_remove(struct i2c_client *client)
  146. {
  147. sysfs_remove_group(&client->dev.kobj, &bh1780_attr_group);
  148. return 0;
  149. }
  150. #ifdef CONFIG_PM_SLEEP
  151. static int bh1780_suspend(struct device *dev)
  152. {
  153. struct bh1780_data *ddata;
  154. int state, ret;
  155. struct i2c_client *client = to_i2c_client(dev);
  156. ddata = i2c_get_clientdata(client);
  157. state = bh1780_read(ddata, BH1780_REG_CONTROL, "CONTROL");
  158. if (state < 0)
  159. return state;
  160. ddata->power_state = state & BH1780_POWMASK;
  161. ret = bh1780_write(ddata, BH1780_REG_CONTROL, BH1780_POFF,
  162. "CONTROL");
  163. if (ret < 0)
  164. return ret;
  165. return 0;
  166. }
  167. static int bh1780_resume(struct device *dev)
  168. {
  169. struct bh1780_data *ddata;
  170. int state, ret;
  171. struct i2c_client *client = to_i2c_client(dev);
  172. ddata = i2c_get_clientdata(client);
  173. state = ddata->power_state;
  174. ret = bh1780_write(ddata, BH1780_REG_CONTROL, state,
  175. "CONTROL");
  176. if (ret < 0)
  177. return ret;
  178. return 0;
  179. }
  180. #endif /* CONFIG_PM_SLEEP */
  181. static SIMPLE_DEV_PM_OPS(bh1780_pm, bh1780_suspend, bh1780_resume);
  182. static const struct i2c_device_id bh1780_id[] = {
  183. { "bh1780", 0 },
  184. { },
  185. };
  186. MODULE_DEVICE_TABLE(i2c, bh1780_id);
  187. #ifdef CONFIG_OF
  188. static const struct of_device_id of_bh1780_match[] = {
  189. { .compatible = "rohm,bh1780gli", },
  190. {},
  191. };
  192. MODULE_DEVICE_TABLE(of, of_bh1780_match);
  193. #endif
  194. static struct i2c_driver bh1780_driver = {
  195. .probe = bh1780_probe,
  196. .remove = bh1780_remove,
  197. .id_table = bh1780_id,
  198. .driver = {
  199. .name = "bh1780",
  200. .pm = &bh1780_pm,
  201. .of_match_table = of_match_ptr(of_bh1780_match),
  202. },
  203. };
  204. module_i2c_driver(bh1780_driver);
  205. MODULE_DESCRIPTION("BH1780GLI Ambient Light Sensor Driver");
  206. MODULE_LICENSE("GPL");
  207. MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");