bh1780gli.c 6.1 KB

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