adt7411.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
  3. *
  4. * Copyright (C) 2008, 2010 Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * TODO: SPI, support for external temperature sensor
  11. * use power-down mode for suspend?, interrupt handling?
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/delay.h>
  18. #include <linux/mutex.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/i2c.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/slab.h>
  24. #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
  25. #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
  26. #define ADT7411_REG_VDD_MSB 0x06
  27. #define ADT7411_REG_INT_TEMP_MSB 0x07
  28. #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
  29. #define ADT7411_REG_CFG1 0x18
  30. #define ADT7411_CFG1_START_MONITOR (1 << 0)
  31. #define ADT7411_REG_CFG2 0x19
  32. #define ADT7411_CFG2_DISABLE_AVG (1 << 5)
  33. #define ADT7411_REG_CFG3 0x1a
  34. #define ADT7411_CFG3_ADC_CLK_225 (1 << 0)
  35. #define ADT7411_CFG3_REF_VDD (1 << 4)
  36. #define ADT7411_REG_DEVICE_ID 0x4d
  37. #define ADT7411_REG_MANUFACTURER_ID 0x4e
  38. #define ADT7411_DEVICE_ID 0x2
  39. #define ADT7411_MANUFACTURER_ID 0x41
  40. static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
  41. struct adt7411_data {
  42. struct mutex device_lock; /* for "atomic" device accesses */
  43. struct mutex update_lock;
  44. unsigned long next_update;
  45. int vref_cached;
  46. struct device *hwmon_dev;
  47. };
  48. /*
  49. * When reading a register containing (up to 4) lsb, all associated
  50. * msb-registers get locked by the hardware. After _one_ of those msb is read,
  51. * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
  52. * is protected here with a mutex, too.
  53. */
  54. static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
  55. u8 msb_reg, u8 lsb_shift)
  56. {
  57. struct adt7411_data *data = i2c_get_clientdata(client);
  58. int val, tmp;
  59. mutex_lock(&data->device_lock);
  60. val = i2c_smbus_read_byte_data(client, lsb_reg);
  61. if (val < 0)
  62. goto exit_unlock;
  63. tmp = (val >> lsb_shift) & 3;
  64. val = i2c_smbus_read_byte_data(client, msb_reg);
  65. if (val >= 0)
  66. val = (val << 2) | tmp;
  67. exit_unlock:
  68. mutex_unlock(&data->device_lock);
  69. return val;
  70. }
  71. static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
  72. bool flag)
  73. {
  74. struct adt7411_data *data = i2c_get_clientdata(client);
  75. int ret, val;
  76. mutex_lock(&data->device_lock);
  77. ret = i2c_smbus_read_byte_data(client, reg);
  78. if (ret < 0)
  79. goto exit_unlock;
  80. if (flag)
  81. val = ret | bit;
  82. else
  83. val = ret & ~bit;
  84. ret = i2c_smbus_write_byte_data(client, reg, val);
  85. exit_unlock:
  86. mutex_unlock(&data->device_lock);
  87. return ret;
  88. }
  89. static ssize_t adt7411_show_vdd(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct i2c_client *client = to_i2c_client(dev);
  93. int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
  94. ADT7411_REG_VDD_MSB, 2);
  95. return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
  96. }
  97. static ssize_t adt7411_show_temp(struct device *dev,
  98. struct device_attribute *attr, char *buf)
  99. {
  100. struct i2c_client *client = to_i2c_client(dev);
  101. int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
  102. ADT7411_REG_INT_TEMP_MSB, 0);
  103. if (val < 0)
  104. return val;
  105. val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
  106. return sprintf(buf, "%d\n", val * 250);
  107. }
  108. static ssize_t adt7411_show_input(struct device *dev,
  109. struct device_attribute *attr, char *buf)
  110. {
  111. int nr = to_sensor_dev_attr(attr)->index;
  112. struct i2c_client *client = to_i2c_client(dev);
  113. struct adt7411_data *data = i2c_get_clientdata(client);
  114. int val;
  115. u8 lsb_reg, lsb_shift;
  116. mutex_lock(&data->update_lock);
  117. if (time_after_eq(jiffies, data->next_update)) {
  118. val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
  119. if (val < 0)
  120. goto exit_unlock;
  121. if (val & ADT7411_CFG3_REF_VDD) {
  122. val = adt7411_read_10_bit(client,
  123. ADT7411_REG_INT_TEMP_VDD_LSB,
  124. ADT7411_REG_VDD_MSB, 2);
  125. if (val < 0)
  126. goto exit_unlock;
  127. data->vref_cached = val * 7000 / 1024;
  128. } else {
  129. data->vref_cached = 2250;
  130. }
  131. data->next_update = jiffies + HZ;
  132. }
  133. lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
  134. lsb_shift = 2 * (nr & 0x03);
  135. val = adt7411_read_10_bit(client, lsb_reg,
  136. ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
  137. if (val < 0)
  138. goto exit_unlock;
  139. val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
  140. exit_unlock:
  141. mutex_unlock(&data->update_lock);
  142. return val;
  143. }
  144. static ssize_t adt7411_show_bit(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  148. struct i2c_client *client = to_i2c_client(dev);
  149. int ret = i2c_smbus_read_byte_data(client, attr2->index);
  150. return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
  151. }
  152. static ssize_t adt7411_set_bit(struct device *dev,
  153. struct device_attribute *attr, const char *buf,
  154. size_t count)
  155. {
  156. struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
  157. struct i2c_client *client = to_i2c_client(dev);
  158. struct adt7411_data *data = i2c_get_clientdata(client);
  159. int ret;
  160. unsigned long flag;
  161. ret = strict_strtoul(buf, 0, &flag);
  162. if (ret || flag > 1)
  163. return -EINVAL;
  164. ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
  165. /* force update */
  166. mutex_lock(&data->update_lock);
  167. data->next_update = jiffies;
  168. mutex_unlock(&data->update_lock);
  169. return ret < 0 ? ret : count;
  170. }
  171. #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
  172. SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
  173. adt7411_set_bit, __bit, __reg)
  174. static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
  175. static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
  176. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
  177. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
  178. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
  179. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
  180. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
  181. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
  182. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
  183. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
  184. static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
  185. static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
  186. static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
  187. static struct attribute *adt7411_attrs[] = {
  188. &dev_attr_temp1_input.attr,
  189. &dev_attr_in0_input.attr,
  190. &sensor_dev_attr_in1_input.dev_attr.attr,
  191. &sensor_dev_attr_in2_input.dev_attr.attr,
  192. &sensor_dev_attr_in3_input.dev_attr.attr,
  193. &sensor_dev_attr_in4_input.dev_attr.attr,
  194. &sensor_dev_attr_in5_input.dev_attr.attr,
  195. &sensor_dev_attr_in6_input.dev_attr.attr,
  196. &sensor_dev_attr_in7_input.dev_attr.attr,
  197. &sensor_dev_attr_in8_input.dev_attr.attr,
  198. &sensor_dev_attr_no_average.dev_attr.attr,
  199. &sensor_dev_attr_fast_sampling.dev_attr.attr,
  200. &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
  201. NULL
  202. };
  203. static const struct attribute_group adt7411_attr_grp = {
  204. .attrs = adt7411_attrs,
  205. };
  206. static int adt7411_detect(struct i2c_client *client,
  207. struct i2c_board_info *info)
  208. {
  209. int val;
  210. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  211. return -ENODEV;
  212. val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
  213. if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
  214. dev_dbg(&client->dev, "Wrong manufacturer ID. Got %d, "
  215. "expected %d\n", val, ADT7411_MANUFACTURER_ID);
  216. return -ENODEV;
  217. }
  218. val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
  219. if (val < 0 || val != ADT7411_DEVICE_ID) {
  220. dev_dbg(&client->dev, "Wrong device ID. Got %d, "
  221. "expected %d\n", val, ADT7411_DEVICE_ID);
  222. return -ENODEV;
  223. }
  224. strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
  225. return 0;
  226. }
  227. static int __devinit adt7411_probe(struct i2c_client *client,
  228. const struct i2c_device_id *id)
  229. {
  230. struct adt7411_data *data;
  231. int ret;
  232. data = kzalloc(sizeof(*data), GFP_KERNEL);
  233. if (!data)
  234. return -ENOMEM;
  235. i2c_set_clientdata(client, data);
  236. mutex_init(&data->device_lock);
  237. mutex_init(&data->update_lock);
  238. ret = adt7411_modify_bit(client, ADT7411_REG_CFG1,
  239. ADT7411_CFG1_START_MONITOR, 1);
  240. if (ret < 0)
  241. goto exit_free;
  242. /* force update on first occasion */
  243. data->next_update = jiffies;
  244. ret = sysfs_create_group(&client->dev.kobj, &adt7411_attr_grp);
  245. if (ret)
  246. goto exit_free;
  247. data->hwmon_dev = hwmon_device_register(&client->dev);
  248. if (IS_ERR(data->hwmon_dev)) {
  249. ret = PTR_ERR(data->hwmon_dev);
  250. goto exit_remove;
  251. }
  252. dev_info(&client->dev, "successfully registered\n");
  253. return 0;
  254. exit_remove:
  255. sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
  256. exit_free:
  257. kfree(data);
  258. return ret;
  259. }
  260. static int __devexit adt7411_remove(struct i2c_client *client)
  261. {
  262. struct adt7411_data *data = i2c_get_clientdata(client);
  263. hwmon_device_unregister(data->hwmon_dev);
  264. sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
  265. kfree(data);
  266. return 0;
  267. }
  268. static const struct i2c_device_id adt7411_id[] = {
  269. { "adt7411", 0 },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(i2c, adt7411_id);
  273. static struct i2c_driver adt7411_driver = {
  274. .driver = {
  275. .name = "adt7411",
  276. },
  277. .probe = adt7411_probe,
  278. .remove = __devexit_p(adt7411_remove),
  279. .id_table = adt7411_id,
  280. .detect = adt7411_detect,
  281. .address_list = normal_i2c,
  282. .class = I2C_CLASS_HWMON,
  283. };
  284. static int __init sensors_adt7411_init(void)
  285. {
  286. return i2c_add_driver(&adt7411_driver);
  287. }
  288. module_init(sensors_adt7411_init)
  289. static void __exit sensors_adt7411_exit(void)
  290. {
  291. i2c_del_driver(&adt7411_driver);
  292. }
  293. module_exit(sensors_adt7411_exit)
  294. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
  295. "Wolfram Sang <w.sang@pengutronix.de>");
  296. MODULE_DESCRIPTION("ADT7411 driver");
  297. MODULE_LICENSE("GPL v2");