adt7411.c 9.4 KB

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