adt7411.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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_STAT_1 0x00
  23. #define ADT7411_STAT_1_INT_TEMP_HIGH BIT(0)
  24. #define ADT7411_STAT_1_INT_TEMP_LOW BIT(1)
  25. #define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1 BIT(2)
  26. #define ADT7411_STAT_1_EXT_TEMP_LOW BIT(3)
  27. #define ADT7411_STAT_1_EXT_TEMP_FAULT BIT(4)
  28. #define ADT7411_STAT_1_AIN2 BIT(5)
  29. #define ADT7411_STAT_1_AIN3 BIT(6)
  30. #define ADT7411_STAT_1_AIN4 BIT(7)
  31. #define ADT7411_REG_STAT_2 0x01
  32. #define ADT7411_STAT_2_AIN5 BIT(0)
  33. #define ADT7411_STAT_2_AIN6 BIT(1)
  34. #define ADT7411_STAT_2_AIN7 BIT(2)
  35. #define ADT7411_STAT_2_AIN8 BIT(3)
  36. #define ADT7411_STAT_2_VDD BIT(4)
  37. #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
  38. #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
  39. #define ADT7411_REG_VDD_MSB 0x06
  40. #define ADT7411_REG_INT_TEMP_MSB 0x07
  41. #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
  42. #define ADT7411_REG_CFG1 0x18
  43. #define ADT7411_CFG1_START_MONITOR BIT(0)
  44. #define ADT7411_CFG1_RESERVED_BIT1 BIT(1)
  45. #define ADT7411_CFG1_EXT_TDM BIT(2)
  46. #define ADT7411_CFG1_RESERVED_BIT3 BIT(3)
  47. #define ADT7411_REG_CFG2 0x19
  48. #define ADT7411_CFG2_DISABLE_AVG BIT(5)
  49. #define ADT7411_REG_CFG3 0x1a
  50. #define ADT7411_CFG3_ADC_CLK_225 BIT(0)
  51. #define ADT7411_CFG3_RESERVED_BIT1 BIT(1)
  52. #define ADT7411_CFG3_RESERVED_BIT2 BIT(2)
  53. #define ADT7411_CFG3_RESERVED_BIT3 BIT(3)
  54. #define ADT7411_CFG3_REF_VDD BIT(4)
  55. #define ADT7411_REG_VDD_HIGH 0x23
  56. #define ADT7411_REG_VDD_LOW 0x24
  57. #define ADT7411_REG_TEMP_HIGH(nr) (0x25 + 2 * (nr))
  58. #define ADT7411_REG_TEMP_LOW(nr) (0x26 + 2 * (nr))
  59. #define ADT7411_REG_IN_HIGH(nr) ((nr) > 1 \
  60. ? 0x2b + 2 * ((nr)-2) \
  61. : 0x27)
  62. #define ADT7411_REG_IN_LOW(nr) ((nr) > 1 \
  63. ? 0x2c + 2 * ((nr)-2) \
  64. : 0x28)
  65. #define ADT7411_REG_DEVICE_ID 0x4d
  66. #define ADT7411_REG_MANUFACTURER_ID 0x4e
  67. #define ADT7411_DEVICE_ID 0x2
  68. #define ADT7411_MANUFACTURER_ID 0x41
  69. static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
  70. static const u8 adt7411_in_alarm_reg[] = {
  71. ADT7411_REG_STAT_2,
  72. ADT7411_REG_STAT_1,
  73. ADT7411_REG_STAT_1,
  74. ADT7411_REG_STAT_1,
  75. ADT7411_REG_STAT_1,
  76. ADT7411_REG_STAT_2,
  77. ADT7411_REG_STAT_2,
  78. ADT7411_REG_STAT_2,
  79. ADT7411_REG_STAT_2,
  80. };
  81. static const u8 adt7411_in_alarm_bits[] = {
  82. ADT7411_STAT_2_VDD,
  83. ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1,
  84. ADT7411_STAT_1_AIN2,
  85. ADT7411_STAT_1_AIN3,
  86. ADT7411_STAT_1_AIN4,
  87. ADT7411_STAT_2_AIN5,
  88. ADT7411_STAT_2_AIN6,
  89. ADT7411_STAT_2_AIN7,
  90. ADT7411_STAT_2_AIN8,
  91. };
  92. struct adt7411_data {
  93. struct mutex device_lock; /* for "atomic" device accesses */
  94. struct mutex update_lock;
  95. unsigned long next_update;
  96. long vref_cached;
  97. struct i2c_client *client;
  98. bool use_ext_temp;
  99. };
  100. /*
  101. * When reading a register containing (up to 4) lsb, all associated
  102. * msb-registers get locked by the hardware. After _one_ of those msb is read,
  103. * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
  104. * is protected here with a mutex, too.
  105. */
  106. static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
  107. u8 msb_reg, u8 lsb_shift)
  108. {
  109. struct adt7411_data *data = i2c_get_clientdata(client);
  110. int val, tmp;
  111. mutex_lock(&data->device_lock);
  112. val = i2c_smbus_read_byte_data(client, lsb_reg);
  113. if (val < 0)
  114. goto exit_unlock;
  115. tmp = (val >> lsb_shift) & 3;
  116. val = i2c_smbus_read_byte_data(client, msb_reg);
  117. if (val >= 0)
  118. val = (val << 2) | tmp;
  119. exit_unlock:
  120. mutex_unlock(&data->device_lock);
  121. return val;
  122. }
  123. static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
  124. bool flag)
  125. {
  126. struct adt7411_data *data = i2c_get_clientdata(client);
  127. int ret, val;
  128. mutex_lock(&data->device_lock);
  129. ret = i2c_smbus_read_byte_data(client, reg);
  130. if (ret < 0)
  131. goto exit_unlock;
  132. if (flag)
  133. val = ret | bit;
  134. else
  135. val = ret & ~bit;
  136. ret = i2c_smbus_write_byte_data(client, reg, val);
  137. exit_unlock:
  138. mutex_unlock(&data->device_lock);
  139. return ret;
  140. }
  141. static ssize_t adt7411_show_bit(struct device *dev,
  142. struct device_attribute *attr, char *buf)
  143. {
  144. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  145. struct adt7411_data *data = dev_get_drvdata(dev);
  146. struct i2c_client *client = data->client;
  147. int ret = i2c_smbus_read_byte_data(client, attr2->index);
  148. return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
  149. }
  150. static ssize_t adt7411_set_bit(struct device *dev,
  151. struct device_attribute *attr, const char *buf,
  152. size_t count)
  153. {
  154. struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
  155. struct adt7411_data *data = dev_get_drvdata(dev);
  156. struct i2c_client *client = data->client;
  157. int ret;
  158. unsigned long flag;
  159. ret = kstrtoul(buf, 0, &flag);
  160. if (ret || flag > 1)
  161. return -EINVAL;
  162. ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
  163. /* force update */
  164. mutex_lock(&data->update_lock);
  165. data->next_update = jiffies;
  166. mutex_unlock(&data->update_lock);
  167. return ret < 0 ? ret : count;
  168. }
  169. #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
  170. SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
  171. adt7411_set_bit, __bit, __reg)
  172. static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
  173. static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
  174. static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
  175. static struct attribute *adt7411_attrs[] = {
  176. &sensor_dev_attr_no_average.dev_attr.attr,
  177. &sensor_dev_attr_fast_sampling.dev_attr.attr,
  178. &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
  179. NULL
  180. };
  181. ATTRIBUTE_GROUPS(adt7411);
  182. static int adt7411_read_in_alarm(struct device *dev, int channel, long *val)
  183. {
  184. struct adt7411_data *data = dev_get_drvdata(dev);
  185. struct i2c_client *client = data->client;
  186. int ret;
  187. ret = i2c_smbus_read_byte_data(client, adt7411_in_alarm_reg[channel]);
  188. if (ret < 0)
  189. return ret;
  190. *val = !!(ret & adt7411_in_alarm_bits[channel]);
  191. return 0;
  192. }
  193. static int adt7411_read_in_vdd(struct device *dev, u32 attr, long *val)
  194. {
  195. struct adt7411_data *data = dev_get_drvdata(dev);
  196. struct i2c_client *client = data->client;
  197. int ret;
  198. switch (attr) {
  199. case hwmon_in_input:
  200. ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
  201. ADT7411_REG_VDD_MSB, 2);
  202. if (ret < 0)
  203. return ret;
  204. *val = ret * 7000 / 1024;
  205. return 0;
  206. case hwmon_in_min:
  207. ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_LOW);
  208. if (ret < 0)
  209. return ret;
  210. *val = ret * 7000 / 256;
  211. return 0;
  212. case hwmon_in_max:
  213. ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_HIGH);
  214. if (ret < 0)
  215. return ret;
  216. *val = ret * 7000 / 256;
  217. return 0;
  218. case hwmon_in_alarm:
  219. return adt7411_read_in_alarm(dev, 0, val);
  220. default:
  221. return -EOPNOTSUPP;
  222. }
  223. }
  224. static int adt7411_update_vref(struct device *dev)
  225. {
  226. struct adt7411_data *data = dev_get_drvdata(dev);
  227. struct i2c_client *client = data->client;
  228. int val;
  229. if (time_after_eq(jiffies, data->next_update)) {
  230. val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
  231. if (val < 0)
  232. return val;
  233. if (val & ADT7411_CFG3_REF_VDD) {
  234. val = adt7411_read_in_vdd(dev, hwmon_in_input,
  235. &data->vref_cached);
  236. if (val < 0)
  237. return val;
  238. } else {
  239. data->vref_cached = 2250;
  240. }
  241. data->next_update = jiffies + HZ;
  242. }
  243. return 0;
  244. }
  245. static int adt7411_read_in_chan(struct device *dev, u32 attr, int channel,
  246. long *val)
  247. {
  248. struct adt7411_data *data = dev_get_drvdata(dev);
  249. struct i2c_client *client = data->client;
  250. int ret;
  251. int reg, lsb_reg, lsb_shift;
  252. int nr = channel - 1;
  253. mutex_lock(&data->update_lock);
  254. ret = adt7411_update_vref(dev);
  255. if (ret < 0)
  256. goto exit_unlock;
  257. switch (attr) {
  258. case hwmon_in_input:
  259. lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
  260. lsb_shift = 2 * (nr & 0x03);
  261. ret = adt7411_read_10_bit(client, lsb_reg,
  262. ADT7411_REG_EXT_TEMP_AIN1_MSB + nr,
  263. lsb_shift);
  264. if (ret < 0)
  265. goto exit_unlock;
  266. *val = ret * data->vref_cached / 1024;
  267. ret = 0;
  268. break;
  269. case hwmon_in_min:
  270. case hwmon_in_max:
  271. reg = (attr == hwmon_in_min)
  272. ? ADT7411_REG_IN_LOW(channel)
  273. : ADT7411_REG_IN_HIGH(channel);
  274. ret = i2c_smbus_read_byte_data(client, reg);
  275. if (ret < 0)
  276. goto exit_unlock;
  277. *val = ret * data->vref_cached / 256;
  278. ret = 0;
  279. break;
  280. case hwmon_in_alarm:
  281. ret = adt7411_read_in_alarm(dev, channel, val);
  282. break;
  283. default:
  284. ret = -EOPNOTSUPP;
  285. break;
  286. }
  287. exit_unlock:
  288. mutex_unlock(&data->update_lock);
  289. return ret;
  290. }
  291. static int adt7411_read_in(struct device *dev, u32 attr, int channel,
  292. long *val)
  293. {
  294. if (channel == 0)
  295. return adt7411_read_in_vdd(dev, attr, val);
  296. else
  297. return adt7411_read_in_chan(dev, attr, channel, val);
  298. }
  299. static int adt7411_read_temp_alarm(struct device *dev, u32 attr, int channel,
  300. long *val)
  301. {
  302. struct adt7411_data *data = dev_get_drvdata(dev);
  303. struct i2c_client *client = data->client;
  304. int ret, bit;
  305. ret = i2c_smbus_read_byte_data(client, ADT7411_REG_STAT_1);
  306. if (ret < 0)
  307. return ret;
  308. switch (attr) {
  309. case hwmon_temp_min_alarm:
  310. bit = channel ? ADT7411_STAT_1_EXT_TEMP_LOW
  311. : ADT7411_STAT_1_INT_TEMP_LOW;
  312. break;
  313. case hwmon_temp_max_alarm:
  314. bit = channel ? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
  315. : ADT7411_STAT_1_INT_TEMP_HIGH;
  316. break;
  317. case hwmon_temp_fault:
  318. bit = ADT7411_STAT_1_EXT_TEMP_FAULT;
  319. break;
  320. default:
  321. return -EOPNOTSUPP;
  322. }
  323. *val = !!(ret & bit);
  324. return 0;
  325. }
  326. static int adt7411_read_temp(struct device *dev, u32 attr, int channel,
  327. long *val)
  328. {
  329. struct adt7411_data *data = dev_get_drvdata(dev);
  330. struct i2c_client *client = data->client;
  331. int ret, reg, regl, regh;
  332. switch (attr) {
  333. case hwmon_temp_input:
  334. regl = channel ? ADT7411_REG_EXT_TEMP_AIN14_LSB :
  335. ADT7411_REG_INT_TEMP_VDD_LSB;
  336. regh = channel ? ADT7411_REG_EXT_TEMP_AIN1_MSB :
  337. ADT7411_REG_INT_TEMP_MSB;
  338. ret = adt7411_read_10_bit(client, regl, regh, 0);
  339. if (ret < 0)
  340. return ret;
  341. ret = ret & 0x200 ? ret - 0x400 : ret; /* 10 bit signed */
  342. *val = ret * 250;
  343. return 0;
  344. case hwmon_temp_min:
  345. case hwmon_temp_max:
  346. reg = (attr == hwmon_temp_min)
  347. ? ADT7411_REG_TEMP_LOW(channel)
  348. : ADT7411_REG_TEMP_HIGH(channel);
  349. ret = i2c_smbus_read_byte_data(client, reg);
  350. if (ret < 0)
  351. return ret;
  352. ret = ret & 0x80 ? ret - 0x100 : ret; /* 8 bit signed */
  353. *val = ret * 1000;
  354. return 0;
  355. case hwmon_temp_min_alarm:
  356. case hwmon_temp_max_alarm:
  357. case hwmon_temp_fault:
  358. return adt7411_read_temp_alarm(dev, attr, channel, val);
  359. default:
  360. return -EOPNOTSUPP;
  361. }
  362. }
  363. static int adt7411_read(struct device *dev, enum hwmon_sensor_types type,
  364. u32 attr, int channel, long *val)
  365. {
  366. switch (type) {
  367. case hwmon_in:
  368. return adt7411_read_in(dev, attr, channel, val);
  369. case hwmon_temp:
  370. return adt7411_read_temp(dev, attr, channel, val);
  371. default:
  372. return -EOPNOTSUPP;
  373. }
  374. }
  375. static int adt7411_write_in_vdd(struct device *dev, u32 attr, long val)
  376. {
  377. struct adt7411_data *data = dev_get_drvdata(dev);
  378. struct i2c_client *client = data->client;
  379. int reg;
  380. val = clamp_val(val, 0, 255 * 7000 / 256);
  381. val = DIV_ROUND_CLOSEST(val * 256, 7000);
  382. switch (attr) {
  383. case hwmon_in_min:
  384. reg = ADT7411_REG_VDD_LOW;
  385. break;
  386. case hwmon_in_max:
  387. reg = ADT7411_REG_VDD_HIGH;
  388. break;
  389. default:
  390. return -EOPNOTSUPP;
  391. }
  392. return i2c_smbus_write_byte_data(client, reg, val);
  393. }
  394. static int adt7411_write_in_chan(struct device *dev, u32 attr, int channel,
  395. long val)
  396. {
  397. struct adt7411_data *data = dev_get_drvdata(dev);
  398. struct i2c_client *client = data->client;
  399. int ret, reg;
  400. mutex_lock(&data->update_lock);
  401. ret = adt7411_update_vref(dev);
  402. if (ret < 0)
  403. goto exit_unlock;
  404. val = clamp_val(val, 0, 255 * data->vref_cached / 256);
  405. val = DIV_ROUND_CLOSEST(val * 256, data->vref_cached);
  406. switch (attr) {
  407. case hwmon_in_min:
  408. reg = ADT7411_REG_IN_LOW(channel);
  409. break;
  410. case hwmon_in_max:
  411. reg = ADT7411_REG_IN_HIGH(channel);
  412. break;
  413. default:
  414. ret = -EOPNOTSUPP;
  415. goto exit_unlock;
  416. }
  417. ret = i2c_smbus_write_byte_data(client, reg, val);
  418. exit_unlock:
  419. mutex_unlock(&data->update_lock);
  420. return ret;
  421. }
  422. static int adt7411_write_in(struct device *dev, u32 attr, int channel,
  423. long val)
  424. {
  425. if (channel == 0)
  426. return adt7411_write_in_vdd(dev, attr, val);
  427. else
  428. return adt7411_write_in_chan(dev, attr, channel, val);
  429. }
  430. static int adt7411_write_temp(struct device *dev, u32 attr, int channel,
  431. long val)
  432. {
  433. struct adt7411_data *data = dev_get_drvdata(dev);
  434. struct i2c_client *client = data->client;
  435. int reg;
  436. val = clamp_val(val, -128000, 127000);
  437. val = DIV_ROUND_CLOSEST(val, 1000);
  438. switch (attr) {
  439. case hwmon_temp_min:
  440. reg = ADT7411_REG_TEMP_LOW(channel);
  441. break;
  442. case hwmon_temp_max:
  443. reg = ADT7411_REG_TEMP_HIGH(channel);
  444. break;
  445. default:
  446. return -EOPNOTSUPP;
  447. }
  448. return i2c_smbus_write_byte_data(client, reg, val);
  449. }
  450. static int adt7411_write(struct device *dev, enum hwmon_sensor_types type,
  451. u32 attr, int channel, long val)
  452. {
  453. switch (type) {
  454. case hwmon_in:
  455. return adt7411_write_in(dev, attr, channel, val);
  456. case hwmon_temp:
  457. return adt7411_write_temp(dev, attr, channel, val);
  458. default:
  459. return -EOPNOTSUPP;
  460. }
  461. }
  462. static umode_t adt7411_is_visible(const void *_data,
  463. enum hwmon_sensor_types type,
  464. u32 attr, int channel)
  465. {
  466. const struct adt7411_data *data = _data;
  467. bool visible;
  468. switch (type) {
  469. case hwmon_in:
  470. visible = channel == 0 || channel >= 3 || !data->use_ext_temp;
  471. switch (attr) {
  472. case hwmon_in_input:
  473. case hwmon_in_alarm:
  474. return visible ? S_IRUGO : 0;
  475. case hwmon_in_min:
  476. case hwmon_in_max:
  477. return visible ? S_IRUGO | S_IWUSR : 0;
  478. }
  479. break;
  480. case hwmon_temp:
  481. visible = channel == 0 || data->use_ext_temp;
  482. switch (attr) {
  483. case hwmon_temp_input:
  484. case hwmon_temp_min_alarm:
  485. case hwmon_temp_max_alarm:
  486. case hwmon_temp_fault:
  487. return visible ? S_IRUGO : 0;
  488. case hwmon_temp_min:
  489. case hwmon_temp_max:
  490. return visible ? S_IRUGO | S_IWUSR : 0;
  491. }
  492. break;
  493. default:
  494. break;
  495. }
  496. return 0;
  497. }
  498. static int adt7411_detect(struct i2c_client *client,
  499. struct i2c_board_info *info)
  500. {
  501. int val;
  502. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  503. return -ENODEV;
  504. val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
  505. if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
  506. dev_dbg(&client->dev,
  507. "Wrong manufacturer ID. Got %d, expected %d\n",
  508. val, ADT7411_MANUFACTURER_ID);
  509. return -ENODEV;
  510. }
  511. val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
  512. if (val < 0 || val != ADT7411_DEVICE_ID) {
  513. dev_dbg(&client->dev,
  514. "Wrong device ID. Got %d, expected %d\n",
  515. val, ADT7411_DEVICE_ID);
  516. return -ENODEV;
  517. }
  518. strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
  519. return 0;
  520. }
  521. static int adt7411_init_device(struct adt7411_data *data)
  522. {
  523. int ret;
  524. u8 val;
  525. ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
  526. if (ret < 0)
  527. return ret;
  528. /*
  529. * We must only write zero to bit 1 and bit 2 and only one to bit 3
  530. * according to the datasheet.
  531. */
  532. val = ret;
  533. val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
  534. val |= ADT7411_CFG3_RESERVED_BIT3;
  535. ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
  536. if (ret < 0)
  537. return ret;
  538. ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
  539. if (ret < 0)
  540. return ret;
  541. data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;
  542. /*
  543. * We must only write zero to bit 1 and only one to bit 3 according to
  544. * the datasheet.
  545. */
  546. val = ret;
  547. val &= ~ADT7411_CFG1_RESERVED_BIT1;
  548. val |= ADT7411_CFG1_RESERVED_BIT3;
  549. /* enable monitoring */
  550. val |= ADT7411_CFG1_START_MONITOR;
  551. return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
  552. }
  553. static const u32 adt7411_in_config[] = {
  554. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  555. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  556. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  557. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  558. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  559. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  560. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  561. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  562. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
  563. 0
  564. };
  565. static const struct hwmon_channel_info adt7411_in = {
  566. .type = hwmon_in,
  567. .config = adt7411_in_config,
  568. };
  569. static const u32 adt7411_temp_config[] = {
  570. HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
  571. HWMON_T_MAX | HWMON_T_MAX_ALARM,
  572. HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
  573. HWMON_T_MAX | HWMON_T_MAX_ALARM | HWMON_T_FAULT,
  574. 0
  575. };
  576. static const struct hwmon_channel_info adt7411_temp = {
  577. .type = hwmon_temp,
  578. .config = adt7411_temp_config,
  579. };
  580. static const struct hwmon_channel_info *adt7411_info[] = {
  581. &adt7411_in,
  582. &adt7411_temp,
  583. NULL
  584. };
  585. static const struct hwmon_ops adt7411_hwmon_ops = {
  586. .is_visible = adt7411_is_visible,
  587. .read = adt7411_read,
  588. .write = adt7411_write,
  589. };
  590. static const struct hwmon_chip_info adt7411_chip_info = {
  591. .ops = &adt7411_hwmon_ops,
  592. .info = adt7411_info,
  593. };
  594. static int adt7411_probe(struct i2c_client *client,
  595. const struct i2c_device_id *id)
  596. {
  597. struct device *dev = &client->dev;
  598. struct adt7411_data *data;
  599. struct device *hwmon_dev;
  600. int ret;
  601. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  602. if (!data)
  603. return -ENOMEM;
  604. i2c_set_clientdata(client, data);
  605. data->client = client;
  606. mutex_init(&data->device_lock);
  607. mutex_init(&data->update_lock);
  608. ret = adt7411_init_device(data);
  609. if (ret < 0)
  610. return ret;
  611. /* force update on first occasion */
  612. data->next_update = jiffies;
  613. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  614. data,
  615. &adt7411_chip_info,
  616. adt7411_groups);
  617. return PTR_ERR_OR_ZERO(hwmon_dev);
  618. }
  619. static const struct i2c_device_id adt7411_id[] = {
  620. { "adt7411", 0 },
  621. { }
  622. };
  623. MODULE_DEVICE_TABLE(i2c, adt7411_id);
  624. static struct i2c_driver adt7411_driver = {
  625. .driver = {
  626. .name = "adt7411",
  627. },
  628. .probe = adt7411_probe,
  629. .id_table = adt7411_id,
  630. .detect = adt7411_detect,
  631. .address_list = normal_i2c,
  632. .class = I2C_CLASS_HWMON,
  633. };
  634. module_i2c_driver(adt7411_driver);
  635. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
  636. "Wolfram Sang <w.sang@pengutronix.de>");
  637. MODULE_DESCRIPTION("ADT7411 driver");
  638. MODULE_LICENSE("GPL v2");