adt7411.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/mutex.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/slab.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 i2c_client *client;
  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 adt7411_data *data = dev_get_drvdata(dev);
  92. struct i2c_client *client = data->client;
  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 adt7411_data *data = dev_get_drvdata(dev);
  101. struct i2c_client *client = data->client;
  102. int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
  103. ADT7411_REG_INT_TEMP_MSB, 0);
  104. if (val < 0)
  105. return val;
  106. val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
  107. return sprintf(buf, "%d\n", val * 250);
  108. }
  109. static ssize_t adt7411_show_input(struct device *dev,
  110. struct device_attribute *attr, char *buf)
  111. {
  112. int nr = to_sensor_dev_attr(attr)->index;
  113. struct adt7411_data *data = dev_get_drvdata(dev);
  114. struct i2c_client *client = data->client;
  115. int val;
  116. u8 lsb_reg, lsb_shift;
  117. mutex_lock(&data->update_lock);
  118. if (time_after_eq(jiffies, data->next_update)) {
  119. val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
  120. if (val < 0)
  121. goto exit_unlock;
  122. if (val & ADT7411_CFG3_REF_VDD) {
  123. val = adt7411_read_10_bit(client,
  124. ADT7411_REG_INT_TEMP_VDD_LSB,
  125. ADT7411_REG_VDD_MSB, 2);
  126. if (val < 0)
  127. goto exit_unlock;
  128. data->vref_cached = val * 7000 / 1024;
  129. } else {
  130. data->vref_cached = 2250;
  131. }
  132. data->next_update = jiffies + HZ;
  133. }
  134. lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
  135. lsb_shift = 2 * (nr & 0x03);
  136. val = adt7411_read_10_bit(client, lsb_reg,
  137. ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
  138. if (val < 0)
  139. goto exit_unlock;
  140. val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
  141. exit_unlock:
  142. mutex_unlock(&data->update_lock);
  143. return val;
  144. }
  145. static ssize_t adt7411_show_bit(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  149. struct adt7411_data *data = dev_get_drvdata(dev);
  150. struct i2c_client *client = data->client;
  151. int ret = i2c_smbus_read_byte_data(client, attr2->index);
  152. return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
  153. }
  154. static ssize_t adt7411_set_bit(struct device *dev,
  155. struct device_attribute *attr, const char *buf,
  156. size_t count)
  157. {
  158. struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
  159. struct adt7411_data *data = dev_get_drvdata(dev);
  160. struct i2c_client *client = data->client;
  161. int ret;
  162. unsigned long flag;
  163. ret = kstrtoul(buf, 0, &flag);
  164. if (ret || flag > 1)
  165. return -EINVAL;
  166. ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
  167. /* force update */
  168. mutex_lock(&data->update_lock);
  169. data->next_update = jiffies;
  170. mutex_unlock(&data->update_lock);
  171. return ret < 0 ? ret : count;
  172. }
  173. #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
  174. SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
  175. adt7411_set_bit, __bit, __reg)
  176. static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
  177. static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
  178. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
  179. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
  180. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
  181. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
  182. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
  183. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
  184. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
  185. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
  186. static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
  187. static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
  188. static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
  189. static struct attribute *adt7411_attrs[] = {
  190. &dev_attr_temp1_input.attr,
  191. &dev_attr_in0_input.attr,
  192. &sensor_dev_attr_in1_input.dev_attr.attr,
  193. &sensor_dev_attr_in2_input.dev_attr.attr,
  194. &sensor_dev_attr_in3_input.dev_attr.attr,
  195. &sensor_dev_attr_in4_input.dev_attr.attr,
  196. &sensor_dev_attr_in5_input.dev_attr.attr,
  197. &sensor_dev_attr_in6_input.dev_attr.attr,
  198. &sensor_dev_attr_in7_input.dev_attr.attr,
  199. &sensor_dev_attr_in8_input.dev_attr.attr,
  200. &sensor_dev_attr_no_average.dev_attr.attr,
  201. &sensor_dev_attr_fast_sampling.dev_attr.attr,
  202. &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
  203. NULL
  204. };
  205. ATTRIBUTE_GROUPS(adt7411);
  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,
  215. "Wrong manufacturer ID. Got %d, expected %d\n",
  216. val, ADT7411_MANUFACTURER_ID);
  217. return -ENODEV;
  218. }
  219. val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
  220. if (val < 0 || val != ADT7411_DEVICE_ID) {
  221. dev_dbg(&client->dev,
  222. "Wrong device ID. Got %d, expected %d\n",
  223. val, ADT7411_DEVICE_ID);
  224. return -ENODEV;
  225. }
  226. strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
  227. return 0;
  228. }
  229. static int adt7411_probe(struct i2c_client *client,
  230. const struct i2c_device_id *id)
  231. {
  232. struct device *dev = &client->dev;
  233. struct adt7411_data *data;
  234. struct device *hwmon_dev;
  235. int ret;
  236. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  237. if (!data)
  238. return -ENOMEM;
  239. i2c_set_clientdata(client, data);
  240. data->client = client;
  241. mutex_init(&data->device_lock);
  242. mutex_init(&data->update_lock);
  243. ret = adt7411_modify_bit(client, ADT7411_REG_CFG1,
  244. ADT7411_CFG1_START_MONITOR, 1);
  245. if (ret < 0)
  246. return ret;
  247. /* force update on first occasion */
  248. data->next_update = jiffies;
  249. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  250. data,
  251. adt7411_groups);
  252. return PTR_ERR_OR_ZERO(hwmon_dev);
  253. }
  254. static const struct i2c_device_id adt7411_id[] = {
  255. { "adt7411", 0 },
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(i2c, adt7411_id);
  259. static struct i2c_driver adt7411_driver = {
  260. .driver = {
  261. .name = "adt7411",
  262. },
  263. .probe = adt7411_probe,
  264. .id_table = adt7411_id,
  265. .detect = adt7411_detect,
  266. .address_list = normal_i2c,
  267. .class = I2C_CLASS_HWMON,
  268. };
  269. module_i2c_driver(adt7411_driver);
  270. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
  271. "Wolfram Sang <w.sang@pengutronix.de>");
  272. MODULE_DESCRIPTION("ADT7411 driver");
  273. MODULE_LICENSE("GPL v2");