bh1780gli.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #define BH1780_REG_CONTROL 0x80
  26. #define BH1780_REG_PARTID 0x8A
  27. #define BH1780_REG_MANFID 0x8B
  28. #define BH1780_REG_DLOW 0x8C
  29. #define BH1780_REG_DHIGH 0x8D
  30. #define BH1780_REVMASK (0xf)
  31. #define BH1780_POWMASK (0x3)
  32. #define BH1780_POFF (0x0)
  33. #define BH1780_PON (0x3)
  34. /* power on settling time in ms */
  35. #define BH1780_PON_DELAY 2
  36. struct bh1780_data {
  37. struct i2c_client *client;
  38. int power_state;
  39. /* lock for sysfs operations */
  40. struct mutex lock;
  41. };
  42. static int bh1780_write(struct bh1780_data *ddata, u8 reg, u8 val, char *msg)
  43. {
  44. int ret = i2c_smbus_write_byte_data(ddata->client, reg, val);
  45. if (ret < 0)
  46. dev_err(&ddata->client->dev,
  47. "i2c_smbus_write_byte_data failed error %d Register (%s)\n",
  48. ret, msg);
  49. return ret;
  50. }
  51. static int bh1780_read(struct bh1780_data *ddata, u8 reg, char *msg)
  52. {
  53. int ret = i2c_smbus_read_byte_data(ddata->client, reg);
  54. if (ret < 0)
  55. dev_err(&ddata->client->dev,
  56. "i2c_smbus_read_byte_data failed error %d Register (%s)\n",
  57. ret, msg);
  58. return ret;
  59. }
  60. static ssize_t bh1780_show_lux(struct device *dev,
  61. struct device_attribute *attr, char *buf)
  62. {
  63. struct platform_device *pdev = to_platform_device(dev);
  64. struct bh1780_data *ddata = platform_get_drvdata(pdev);
  65. int lsb, msb;
  66. lsb = bh1780_read(ddata, BH1780_REG_DLOW, "DLOW");
  67. if (lsb < 0)
  68. return lsb;
  69. msb = bh1780_read(ddata, BH1780_REG_DHIGH, "DHIGH");
  70. if (msb < 0)
  71. return msb;
  72. return sprintf(buf, "%d\n", (msb << 8) | lsb);
  73. }
  74. static ssize_t bh1780_show_power_state(struct device *dev,
  75. struct device_attribute *attr,
  76. char *buf)
  77. {
  78. struct platform_device *pdev = to_platform_device(dev);
  79. struct bh1780_data *ddata = platform_get_drvdata(pdev);
  80. int state;
  81. state = bh1780_read(ddata, BH1780_REG_CONTROL, "CONTROL");
  82. if (state < 0)
  83. return state;
  84. return sprintf(buf, "%d\n", state & BH1780_POWMASK);
  85. }
  86. static ssize_t bh1780_store_power_state(struct device *dev,
  87. struct device_attribute *attr,
  88. const char *buf, size_t count)
  89. {
  90. struct platform_device *pdev = to_platform_device(dev);
  91. struct bh1780_data *ddata = platform_get_drvdata(pdev);
  92. unsigned long val;
  93. int error;
  94. error = strict_strtoul(buf, 0, &val);
  95. if (error)
  96. return error;
  97. if (val < BH1780_POFF || val > BH1780_PON)
  98. return -EINVAL;
  99. mutex_lock(&ddata->lock);
  100. error = bh1780_write(ddata, BH1780_REG_CONTROL, val, "CONTROL");
  101. if (error < 0) {
  102. mutex_unlock(&ddata->lock);
  103. return error;
  104. }
  105. msleep(BH1780_PON_DELAY);
  106. ddata->power_state = val;
  107. mutex_unlock(&ddata->lock);
  108. return count;
  109. }
  110. static DEVICE_ATTR(lux, S_IRUGO, bh1780_show_lux, NULL);
  111. static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
  112. bh1780_show_power_state, bh1780_store_power_state);
  113. static struct attribute *bh1780_attributes[] = {
  114. &dev_attr_power_state.attr,
  115. &dev_attr_lux.attr,
  116. NULL
  117. };
  118. static const struct attribute_group bh1780_attr_group = {
  119. .attrs = bh1780_attributes,
  120. };
  121. static int __devinit bh1780_probe(struct i2c_client *client,
  122. const struct i2c_device_id *id)
  123. {
  124. int ret;
  125. struct bh1780_data *ddata = NULL;
  126. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  127. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) {
  128. ret = -EIO;
  129. goto err_op_failed;
  130. }
  131. ddata = kzalloc(sizeof(struct bh1780_data), GFP_KERNEL);
  132. if (ddata == NULL) {
  133. ret = -ENOMEM;
  134. goto err_op_failed;
  135. }
  136. ddata->client = client;
  137. i2c_set_clientdata(client, ddata);
  138. ret = bh1780_read(ddata, BH1780_REG_PARTID, "PART ID");
  139. if (ret < 0)
  140. goto err_op_failed;
  141. dev_info(&client->dev, "Ambient Light Sensor, Rev : %d\n",
  142. (ret & BH1780_REVMASK));
  143. mutex_init(&ddata->lock);
  144. ret = sysfs_create_group(&client->dev.kobj, &bh1780_attr_group);
  145. if (ret)
  146. goto err_op_failed;
  147. return 0;
  148. err_op_failed:
  149. kfree(ddata);
  150. return ret;
  151. }
  152. static int __devexit bh1780_remove(struct i2c_client *client)
  153. {
  154. struct bh1780_data *ddata;
  155. ddata = i2c_get_clientdata(client);
  156. sysfs_remove_group(&client->dev.kobj, &bh1780_attr_group);
  157. kfree(ddata);
  158. return 0;
  159. }
  160. #ifdef CONFIG_PM
  161. static int bh1780_suspend(struct device *dev)
  162. {
  163. struct bh1780_data *ddata;
  164. int state, ret;
  165. struct i2c_client *client = to_i2c_client(dev);
  166. ddata = i2c_get_clientdata(client);
  167. state = bh1780_read(ddata, BH1780_REG_CONTROL, "CONTROL");
  168. if (state < 0)
  169. return state;
  170. ddata->power_state = state & BH1780_POWMASK;
  171. ret = bh1780_write(ddata, BH1780_REG_CONTROL, BH1780_POFF,
  172. "CONTROL");
  173. if (ret < 0)
  174. return ret;
  175. return 0;
  176. }
  177. static int bh1780_resume(struct device *dev)
  178. {
  179. struct bh1780_data *ddata;
  180. int state, ret;
  181. struct i2c_client *client = to_i2c_client(dev);
  182. ddata = i2c_get_clientdata(client);
  183. state = ddata->power_state;
  184. ret = bh1780_write(ddata, BH1780_REG_CONTROL, state,
  185. "CONTROL");
  186. if (ret < 0)
  187. return ret;
  188. return 0;
  189. }
  190. static SIMPLE_DEV_PM_OPS(bh1780_pm, bh1780_suspend, bh1780_resume);
  191. #define BH1780_PMOPS (&bh1780_pm)
  192. #else
  193. #define BH1780_PMOPS NULL
  194. #endif /* CONFIG_PM */
  195. static const struct i2c_device_id bh1780_id[] = {
  196. { "bh1780", 0 },
  197. { },
  198. };
  199. static struct i2c_driver bh1780_driver = {
  200. .probe = bh1780_probe,
  201. .remove = bh1780_remove,
  202. .id_table = bh1780_id,
  203. .driver = {
  204. .name = "bh1780",
  205. .pm = BH1780_PMOPS,
  206. },
  207. };
  208. static int __init bh1780_init(void)
  209. {
  210. return i2c_add_driver(&bh1780_driver);
  211. }
  212. static void __exit bh1780_exit(void)
  213. {
  214. i2c_del_driver(&bh1780_driver);
  215. }
  216. module_init(bh1780_init)
  217. module_exit(bh1780_exit)
  218. MODULE_DESCRIPTION("BH1780GLI Ambient Light Sensor Driver");
  219. MODULE_LICENSE("GPL");
  220. MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");