adt7411.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
  24. #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
  25. #define ADT7411_REG_VDD_MSB 0x06
  26. #define ADT7411_REG_INT_TEMP_MSB 0x07
  27. #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
  28. #define ADT7411_REG_CFG1 0x18
  29. #define ADT7411_CFG1_START_MONITOR (1 << 0)
  30. #define ADT7411_REG_CFG2 0x19
  31. #define ADT7411_CFG2_DISABLE_AVG (1 << 5)
  32. #define ADT7411_REG_CFG3 0x1a
  33. #define ADT7411_CFG3_ADC_CLK_225 (1 << 0)
  34. #define ADT7411_CFG3_REF_VDD (1 << 4)
  35. #define ADT7411_REG_DEVICE_ID 0x4d
  36. #define ADT7411_REG_MANUFACTURER_ID 0x4e
  37. #define ADT7411_DEVICE_ID 0x2
  38. #define ADT7411_MANUFACTURER_ID 0x41
  39. static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
  40. struct adt7411_data {
  41. struct mutex device_lock; /* for "atomic" device accesses */
  42. struct mutex update_lock;
  43. unsigned long next_update;
  44. int vref_cached;
  45. struct device *hwmon_dev;
  46. };
  47. /*
  48. * When reading a register containing (up to 4) lsb, all associated
  49. * msb-registers get locked by the hardware. After _one_ of those msb is read,
  50. * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
  51. * is protected here with a mutex, too.
  52. */
  53. static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
  54. u8 msb_reg, u8 lsb_shift)
  55. {
  56. struct adt7411_data *data = i2c_get_clientdata(client);
  57. int val, tmp;
  58. mutex_lock(&data->device_lock);
  59. val = i2c_smbus_read_byte_data(client, lsb_reg);
  60. if (val < 0)
  61. goto exit_unlock;
  62. tmp = (val >> lsb_shift) & 3;
  63. val = i2c_smbus_read_byte_data(client, msb_reg);
  64. if (val >= 0)
  65. val = (val << 2) | tmp;
  66. exit_unlock:
  67. mutex_unlock(&data->device_lock);
  68. return val;
  69. }
  70. static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
  71. bool flag)
  72. {
  73. struct adt7411_data *data = i2c_get_clientdata(client);
  74. int ret, val;
  75. mutex_lock(&data->device_lock);
  76. ret = i2c_smbus_read_byte_data(client, reg);
  77. if (ret < 0)
  78. goto exit_unlock;
  79. if (flag)
  80. val = ret | bit;
  81. else
  82. val = ret & ~bit;
  83. ret = i2c_smbus_write_byte_data(client, reg, val);
  84. exit_unlock:
  85. mutex_unlock(&data->device_lock);
  86. return ret;
  87. }
  88. static ssize_t adt7411_show_vdd(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. struct i2c_client *client = to_i2c_client(dev);
  92. int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
  93. ADT7411_REG_VDD_MSB, 2);
  94. return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
  95. }
  96. static ssize_t adt7411_show_temp(struct device *dev,
  97. struct device_attribute *attr, char *buf)
  98. {
  99. struct i2c_client *client = to_i2c_client(dev);
  100. int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
  101. ADT7411_REG_INT_TEMP_MSB, 0);
  102. if (val < 0)
  103. return val;
  104. val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
  105. return sprintf(buf, "%d\n", val * 250);
  106. }
  107. static ssize_t adt7411_show_input(struct device *dev,
  108. struct device_attribute *attr, char *buf)
  109. {
  110. int nr = to_sensor_dev_attr(attr)->index;
  111. struct i2c_client *client = to_i2c_client(dev);
  112. struct adt7411_data *data = i2c_get_clientdata(client);
  113. int val;
  114. u8 lsb_reg, lsb_shift;
  115. mutex_lock(&data->update_lock);
  116. if (time_after_eq(jiffies, data->next_update)) {
  117. val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
  118. if (val < 0)
  119. goto exit_unlock;
  120. if (val & ADT7411_CFG3_REF_VDD) {
  121. val = adt7411_read_10_bit(client,
  122. ADT7411_REG_INT_TEMP_VDD_LSB,
  123. ADT7411_REG_VDD_MSB, 2);
  124. if (val < 0)
  125. goto exit_unlock;
  126. data->vref_cached = val * 7000 / 1024;
  127. } else {
  128. data->vref_cached = 2250;
  129. }
  130. data->next_update = jiffies + HZ;
  131. }
  132. lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
  133. lsb_shift = 2 * (nr & 0x03);
  134. val = adt7411_read_10_bit(client, lsb_reg,
  135. ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
  136. if (val < 0)
  137. goto exit_unlock;
  138. val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
  139. exit_unlock:
  140. mutex_unlock(&data->update_lock);
  141. return val;
  142. }
  143. static ssize_t adt7411_show_bit(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  147. struct i2c_client *client = to_i2c_client(dev);
  148. int ret = i2c_smbus_read_byte_data(client, attr2->index);
  149. return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
  150. }
  151. static ssize_t adt7411_set_bit(struct device *dev,
  152. struct device_attribute *attr, const char *buf,
  153. size_t count)
  154. {
  155. struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
  156. struct i2c_client *client = to_i2c_client(dev);
  157. struct adt7411_data *data = i2c_get_clientdata(client);
  158. int ret;
  159. unsigned long flag;
  160. ret = strict_strtoul(buf, 0, &flag);
  161. if (ret || flag > 1)
  162. return -EINVAL;
  163. ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
  164. /* force update */
  165. mutex_lock(&data->update_lock);
  166. data->next_update = jiffies;
  167. mutex_unlock(&data->update_lock);
  168. return ret < 0 ? ret : count;
  169. }
  170. #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
  171. SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
  172. adt7411_set_bit, __bit, __reg)
  173. static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
  174. static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
  175. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
  176. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
  177. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
  178. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
  179. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
  180. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
  181. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
  182. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
  183. static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
  184. static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
  185. static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
  186. static struct attribute *adt7411_attrs[] = {
  187. &dev_attr_temp1_input.attr,
  188. &dev_attr_in0_input.attr,
  189. &sensor_dev_attr_in1_input.dev_attr.attr,
  190. &sensor_dev_attr_in2_input.dev_attr.attr,
  191. &sensor_dev_attr_in3_input.dev_attr.attr,
  192. &sensor_dev_attr_in4_input.dev_attr.attr,
  193. &sensor_dev_attr_in5_input.dev_attr.attr,
  194. &sensor_dev_attr_in6_input.dev_attr.attr,
  195. &sensor_dev_attr_in7_input.dev_attr.attr,
  196. &sensor_dev_attr_in8_input.dev_attr.attr,
  197. &sensor_dev_attr_no_average.dev_attr.attr,
  198. &sensor_dev_attr_fast_sampling.dev_attr.attr,
  199. &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
  200. NULL
  201. };
  202. static const struct attribute_group adt7411_attr_grp = {
  203. .attrs = adt7411_attrs,
  204. };
  205. static int adt7411_detect(struct i2c_client *client,
  206. struct i2c_board_info *info)
  207. {
  208. int val;
  209. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  210. return -ENODEV;
  211. val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
  212. if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
  213. dev_dbg(&client->dev, "Wrong manufacturer ID. Got %d, "
  214. "expected %d\n", val, ADT7411_MANUFACTURER_ID);
  215. return -ENODEV;
  216. }
  217. val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
  218. if (val < 0 || val != ADT7411_DEVICE_ID) {
  219. dev_dbg(&client->dev, "Wrong device ID. Got %d, "
  220. "expected %d\n", val, ADT7411_DEVICE_ID);
  221. return -ENODEV;
  222. }
  223. strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
  224. return 0;
  225. }
  226. static int __devinit adt7411_probe(struct i2c_client *client,
  227. const struct i2c_device_id *id)
  228. {
  229. struct adt7411_data *data;
  230. int ret;
  231. data = kzalloc(sizeof(*data), GFP_KERNEL);
  232. if (!data)
  233. return -ENOMEM;
  234. i2c_set_clientdata(client, data);
  235. mutex_init(&data->device_lock);
  236. mutex_init(&data->update_lock);
  237. ret = adt7411_modify_bit(client, ADT7411_REG_CFG1,
  238. ADT7411_CFG1_START_MONITOR, 1);
  239. if (ret < 0)
  240. goto exit_free;
  241. /* force update on first occasion */
  242. data->next_update = jiffies;
  243. ret = sysfs_create_group(&client->dev.kobj, &adt7411_attr_grp);
  244. if (ret)
  245. goto exit_free;
  246. data->hwmon_dev = hwmon_device_register(&client->dev);
  247. if (IS_ERR(data->hwmon_dev)) {
  248. ret = PTR_ERR(data->hwmon_dev);
  249. goto exit_remove;
  250. }
  251. dev_info(&client->dev, "successfully registered\n");
  252. return 0;
  253. exit_remove:
  254. sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
  255. exit_free:
  256. i2c_set_clientdata(client, NULL);
  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. i2c_set_clientdata(client, NULL);
  266. kfree(data);
  267. return 0;
  268. }
  269. static const struct i2c_device_id adt7411_id[] = {
  270. { "adt7411", 0 },
  271. { }
  272. };
  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");