adt7411.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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, use power-down mode for suspend?, interrupt handling?
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/i2c.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/hwmon-sysfs.h>
  21. #include <linux/slab.h>
  22. #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
  23. #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
  24. #define ADT7411_REG_VDD_MSB 0x06
  25. #define ADT7411_REG_INT_TEMP_MSB 0x07
  26. #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
  27. #define ADT7411_REG_CFG1 0x18
  28. #define ADT7411_CFG1_START_MONITOR (1 << 0)
  29. #define ADT7411_CFG1_RESERVED_BIT1 (1 << 1)
  30. #define ADT7411_CFG1_EXT_TDM (1 << 2)
  31. #define ADT7411_CFG1_RESERVED_BIT3 (1 << 3)
  32. #define ADT7411_REG_CFG2 0x19
  33. #define ADT7411_CFG2_DISABLE_AVG (1 << 5)
  34. #define ADT7411_REG_CFG3 0x1a
  35. #define ADT7411_CFG3_ADC_CLK_225 (1 << 0)
  36. #define ADT7411_CFG3_RESERVED_BIT1 (1 << 1)
  37. #define ADT7411_CFG3_RESERVED_BIT2 (1 << 2)
  38. #define ADT7411_CFG3_RESERVED_BIT3 (1 << 3)
  39. #define ADT7411_CFG3_REF_VDD (1 << 4)
  40. #define ADT7411_REG_DEVICE_ID 0x4d
  41. #define ADT7411_REG_MANUFACTURER_ID 0x4e
  42. #define ADT7411_DEVICE_ID 0x2
  43. #define ADT7411_MANUFACTURER_ID 0x41
  44. static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
  45. struct adt7411_data {
  46. struct mutex device_lock; /* for "atomic" device accesses */
  47. struct mutex update_lock;
  48. unsigned long next_update;
  49. int vref_cached;
  50. struct i2c_client *client;
  51. bool use_ext_temp;
  52. };
  53. /*
  54. * When reading a register containing (up to 4) lsb, all associated
  55. * msb-registers get locked by the hardware. After _one_ of those msb is read,
  56. * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
  57. * is protected here with a mutex, too.
  58. */
  59. static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
  60. u8 msb_reg, u8 lsb_shift)
  61. {
  62. struct adt7411_data *data = i2c_get_clientdata(client);
  63. int val, tmp;
  64. mutex_lock(&data->device_lock);
  65. val = i2c_smbus_read_byte_data(client, lsb_reg);
  66. if (val < 0)
  67. goto exit_unlock;
  68. tmp = (val >> lsb_shift) & 3;
  69. val = i2c_smbus_read_byte_data(client, msb_reg);
  70. if (val >= 0)
  71. val = (val << 2) | tmp;
  72. exit_unlock:
  73. mutex_unlock(&data->device_lock);
  74. return val;
  75. }
  76. static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
  77. bool flag)
  78. {
  79. struct adt7411_data *data = i2c_get_clientdata(client);
  80. int ret, val;
  81. mutex_lock(&data->device_lock);
  82. ret = i2c_smbus_read_byte_data(client, reg);
  83. if (ret < 0)
  84. goto exit_unlock;
  85. if (flag)
  86. val = ret | bit;
  87. else
  88. val = ret & ~bit;
  89. ret = i2c_smbus_write_byte_data(client, reg, val);
  90. exit_unlock:
  91. mutex_unlock(&data->device_lock);
  92. return ret;
  93. }
  94. static ssize_t adt7411_show_vdd(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. struct adt7411_data *data = dev_get_drvdata(dev);
  98. struct i2c_client *client = data->client;
  99. int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
  100. ADT7411_REG_VDD_MSB, 2);
  101. return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
  102. }
  103. static ssize_t adt7411_show_temp(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. int nr = to_sensor_dev_attr(attr)->index;
  107. struct adt7411_data *data = dev_get_drvdata(dev);
  108. struct i2c_client *client = data->client;
  109. int val;
  110. struct {
  111. u8 low;
  112. u8 high;
  113. } reg[2] = {
  114. { ADT7411_REG_INT_TEMP_VDD_LSB, ADT7411_REG_INT_TEMP_MSB },
  115. { ADT7411_REG_EXT_TEMP_AIN14_LSB,
  116. ADT7411_REG_EXT_TEMP_AIN1_MSB },
  117. };
  118. val = adt7411_read_10_bit(client, reg[nr].low, reg[nr].high, 0);
  119. if (val < 0)
  120. return val;
  121. val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
  122. return sprintf(buf, "%d\n", val * 250);
  123. }
  124. static ssize_t adt7411_show_input(struct device *dev,
  125. struct device_attribute *attr, char *buf)
  126. {
  127. int nr = to_sensor_dev_attr(attr)->index;
  128. struct adt7411_data *data = dev_get_drvdata(dev);
  129. struct i2c_client *client = data->client;
  130. int val;
  131. u8 lsb_reg, lsb_shift;
  132. mutex_lock(&data->update_lock);
  133. if (time_after_eq(jiffies, data->next_update)) {
  134. val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
  135. if (val < 0)
  136. goto exit_unlock;
  137. if (val & ADT7411_CFG3_REF_VDD) {
  138. val = adt7411_read_10_bit(client,
  139. ADT7411_REG_INT_TEMP_VDD_LSB,
  140. ADT7411_REG_VDD_MSB, 2);
  141. if (val < 0)
  142. goto exit_unlock;
  143. data->vref_cached = val * 7000 / 1024;
  144. } else {
  145. data->vref_cached = 2250;
  146. }
  147. data->next_update = jiffies + HZ;
  148. }
  149. lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
  150. lsb_shift = 2 * (nr & 0x03);
  151. val = adt7411_read_10_bit(client, lsb_reg,
  152. ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
  153. if (val < 0)
  154. goto exit_unlock;
  155. val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
  156. exit_unlock:
  157. mutex_unlock(&data->update_lock);
  158. return val;
  159. }
  160. static ssize_t adt7411_show_bit(struct device *dev,
  161. struct device_attribute *attr, char *buf)
  162. {
  163. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  164. struct adt7411_data *data = dev_get_drvdata(dev);
  165. struct i2c_client *client = data->client;
  166. int ret = i2c_smbus_read_byte_data(client, attr2->index);
  167. return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
  168. }
  169. static ssize_t adt7411_set_bit(struct device *dev,
  170. struct device_attribute *attr, const char *buf,
  171. size_t count)
  172. {
  173. struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
  174. struct adt7411_data *data = dev_get_drvdata(dev);
  175. struct i2c_client *client = data->client;
  176. int ret;
  177. unsigned long flag;
  178. ret = kstrtoul(buf, 0, &flag);
  179. if (ret || flag > 1)
  180. return -EINVAL;
  181. ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
  182. /* force update */
  183. mutex_lock(&data->update_lock);
  184. data->next_update = jiffies;
  185. mutex_unlock(&data->update_lock);
  186. return ret < 0 ? ret : count;
  187. }
  188. #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
  189. SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
  190. adt7411_set_bit, __bit, __reg)
  191. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL, 0);
  192. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, adt7411_show_temp, NULL, 1);
  193. static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
  194. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
  195. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
  196. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
  197. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
  198. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
  199. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
  200. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
  201. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
  202. static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
  203. static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
  204. static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
  205. static struct attribute *adt7411_attrs[] = {
  206. &sensor_dev_attr_temp1_input.dev_attr.attr,
  207. &sensor_dev_attr_temp2_input.dev_attr.attr,
  208. &dev_attr_in0_input.attr,
  209. &sensor_dev_attr_in1_input.dev_attr.attr,
  210. &sensor_dev_attr_in2_input.dev_attr.attr,
  211. &sensor_dev_attr_in3_input.dev_attr.attr,
  212. &sensor_dev_attr_in4_input.dev_attr.attr,
  213. &sensor_dev_attr_in5_input.dev_attr.attr,
  214. &sensor_dev_attr_in6_input.dev_attr.attr,
  215. &sensor_dev_attr_in7_input.dev_attr.attr,
  216. &sensor_dev_attr_in8_input.dev_attr.attr,
  217. &sensor_dev_attr_no_average.dev_attr.attr,
  218. &sensor_dev_attr_fast_sampling.dev_attr.attr,
  219. &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
  220. NULL
  221. };
  222. static umode_t adt7411_attrs_visible(struct kobject *kobj,
  223. struct attribute *attr, int index)
  224. {
  225. struct device *dev = container_of(kobj, struct device, kobj);
  226. struct adt7411_data *data = dev_get_drvdata(dev);
  227. bool visible = true;
  228. if (attr == &sensor_dev_attr_temp2_input.dev_attr.attr)
  229. visible = data->use_ext_temp;
  230. else if (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
  231. attr == &sensor_dev_attr_in2_input.dev_attr.attr)
  232. visible = !data->use_ext_temp;
  233. return visible ? attr->mode : 0;
  234. }
  235. static const struct attribute_group adt7411_group = {
  236. .attrs = adt7411_attrs,
  237. .is_visible = adt7411_attrs_visible,
  238. };
  239. __ATTRIBUTE_GROUPS(adt7411);
  240. static int adt7411_detect(struct i2c_client *client,
  241. struct i2c_board_info *info)
  242. {
  243. int val;
  244. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  245. return -ENODEV;
  246. val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
  247. if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
  248. dev_dbg(&client->dev,
  249. "Wrong manufacturer ID. Got %d, expected %d\n",
  250. val, ADT7411_MANUFACTURER_ID);
  251. return -ENODEV;
  252. }
  253. val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
  254. if (val < 0 || val != ADT7411_DEVICE_ID) {
  255. dev_dbg(&client->dev,
  256. "Wrong device ID. Got %d, expected %d\n",
  257. val, ADT7411_DEVICE_ID);
  258. return -ENODEV;
  259. }
  260. strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
  261. return 0;
  262. }
  263. static int adt7411_init_device(struct adt7411_data *data)
  264. {
  265. int ret;
  266. u8 val;
  267. ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
  268. if (ret < 0)
  269. return ret;
  270. /*
  271. * We must only write zero to bit 1 and bit 2 and only one to bit 3
  272. * according to the datasheet.
  273. */
  274. val = ret;
  275. val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
  276. val |= ADT7411_CFG3_RESERVED_BIT3;
  277. ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
  278. if (ret < 0)
  279. return ret;
  280. ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
  281. if (ret < 0)
  282. return ret;
  283. data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;
  284. /*
  285. * We must only write zero to bit 1 and only one to bit 3 according to
  286. * the datasheet.
  287. */
  288. val = ret;
  289. val &= ~ADT7411_CFG1_RESERVED_BIT1;
  290. val |= ADT7411_CFG1_RESERVED_BIT3;
  291. /* enable monitoring */
  292. val |= ADT7411_CFG1_START_MONITOR;
  293. return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
  294. }
  295. static int adt7411_probe(struct i2c_client *client,
  296. const struct i2c_device_id *id)
  297. {
  298. struct device *dev = &client->dev;
  299. struct adt7411_data *data;
  300. struct device *hwmon_dev;
  301. int ret;
  302. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  303. if (!data)
  304. return -ENOMEM;
  305. i2c_set_clientdata(client, data);
  306. data->client = client;
  307. mutex_init(&data->device_lock);
  308. mutex_init(&data->update_lock);
  309. ret = adt7411_init_device(data);
  310. if (ret < 0)
  311. return ret;
  312. /* force update on first occasion */
  313. data->next_update = jiffies;
  314. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  315. data,
  316. adt7411_groups);
  317. return PTR_ERR_OR_ZERO(hwmon_dev);
  318. }
  319. static const struct i2c_device_id adt7411_id[] = {
  320. { "adt7411", 0 },
  321. { }
  322. };
  323. MODULE_DEVICE_TABLE(i2c, adt7411_id);
  324. static struct i2c_driver adt7411_driver = {
  325. .driver = {
  326. .name = "adt7411",
  327. },
  328. .probe = adt7411_probe,
  329. .id_table = adt7411_id,
  330. .detect = adt7411_detect,
  331. .address_list = normal_i2c,
  332. .class = I2C_CLASS_HWMON,
  333. };
  334. module_i2c_driver(adt7411_driver);
  335. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
  336. "Wolfram Sang <w.sang@pengutronix.de>");
  337. MODULE_DESCRIPTION("ADT7411 driver");
  338. MODULE_LICENSE("GPL v2");