tmp401.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /* tmp401.c
  2. *
  3. * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
  4. * Preliminary tmp411 support by:
  5. * Gabriel Konat, Sander Leget, Wouter Willems
  6. * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
  7. *
  8. * Cleanup and support for TMP431 and TMP432 by Guenter Roeck
  9. * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*
  26. * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
  27. *
  28. * Note this IC is in some aspect similar to the LM90, but it has quite a
  29. * few differences too, for example the local temp has a higher resolution
  30. * and thus has 16 bits registers for its value and limit instead of 8 bits.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/bitops.h>
  35. #include <linux/slab.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/i2c.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/err.h>
  41. #include <linux/mutex.h>
  42. #include <linux/sysfs.h>
  43. /* Addresses to scan */
  44. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d,
  45. 0x4e, 0x4f, I2C_CLIENT_END };
  46. enum chips { tmp401, tmp411, tmp431, tmp432, tmp435, tmp461 };
  47. /*
  48. * The TMP401 registers, note some registers have different addresses for
  49. * reading and writing
  50. */
  51. #define TMP401_STATUS 0x02
  52. #define TMP401_CONFIG_READ 0x03
  53. #define TMP401_CONFIG_WRITE 0x09
  54. #define TMP401_CONVERSION_RATE_READ 0x04
  55. #define TMP401_CONVERSION_RATE_WRITE 0x0A
  56. #define TMP401_TEMP_CRIT_HYST 0x21
  57. #define TMP401_MANUFACTURER_ID_REG 0xFE
  58. #define TMP401_DEVICE_ID_REG 0xFF
  59. static const u8 TMP401_TEMP_MSB_READ[7][2] = {
  60. { 0x00, 0x01 }, /* temp */
  61. { 0x06, 0x08 }, /* low limit */
  62. { 0x05, 0x07 }, /* high limit */
  63. { 0x20, 0x19 }, /* therm (crit) limit */
  64. { 0x30, 0x34 }, /* lowest */
  65. { 0x32, 0x36 }, /* highest */
  66. { 0, 0x11 }, /* offset */
  67. };
  68. static const u8 TMP401_TEMP_MSB_WRITE[7][2] = {
  69. { 0, 0 }, /* temp (unused) */
  70. { 0x0C, 0x0E }, /* low limit */
  71. { 0x0B, 0x0D }, /* high limit */
  72. { 0x20, 0x19 }, /* therm (crit) limit */
  73. { 0x30, 0x34 }, /* lowest */
  74. { 0x32, 0x36 }, /* highest */
  75. { 0, 0x11 }, /* offset */
  76. };
  77. static const u8 TMP432_TEMP_MSB_READ[4][3] = {
  78. { 0x00, 0x01, 0x23 }, /* temp */
  79. { 0x06, 0x08, 0x16 }, /* low limit */
  80. { 0x05, 0x07, 0x15 }, /* high limit */
  81. { 0x20, 0x19, 0x1A }, /* therm (crit) limit */
  82. };
  83. static const u8 TMP432_TEMP_MSB_WRITE[4][3] = {
  84. { 0, 0, 0 }, /* temp - unused */
  85. { 0x0C, 0x0E, 0x16 }, /* low limit */
  86. { 0x0B, 0x0D, 0x15 }, /* high limit */
  87. { 0x20, 0x19, 0x1A }, /* therm (crit) limit */
  88. };
  89. /* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */
  90. static const u8 TMP432_STATUS_REG[] = {
  91. 0x1b, 0x36, 0x35, 0x37 };
  92. /* Flags */
  93. #define TMP401_CONFIG_RANGE BIT(2)
  94. #define TMP401_CONFIG_SHUTDOWN BIT(6)
  95. #define TMP401_STATUS_LOCAL_CRIT BIT(0)
  96. #define TMP401_STATUS_REMOTE_CRIT BIT(1)
  97. #define TMP401_STATUS_REMOTE_OPEN BIT(2)
  98. #define TMP401_STATUS_REMOTE_LOW BIT(3)
  99. #define TMP401_STATUS_REMOTE_HIGH BIT(4)
  100. #define TMP401_STATUS_LOCAL_LOW BIT(5)
  101. #define TMP401_STATUS_LOCAL_HIGH BIT(6)
  102. /* On TMP432, each status has its own register */
  103. #define TMP432_STATUS_LOCAL BIT(0)
  104. #define TMP432_STATUS_REMOTE1 BIT(1)
  105. #define TMP432_STATUS_REMOTE2 BIT(2)
  106. /* Manufacturer / Device ID's */
  107. #define TMP401_MANUFACTURER_ID 0x55
  108. #define TMP401_DEVICE_ID 0x11
  109. #define TMP411A_DEVICE_ID 0x12
  110. #define TMP411B_DEVICE_ID 0x13
  111. #define TMP411C_DEVICE_ID 0x10
  112. #define TMP431_DEVICE_ID 0x31
  113. #define TMP432_DEVICE_ID 0x32
  114. #define TMP435_DEVICE_ID 0x35
  115. /*
  116. * Driver data (common to all clients)
  117. */
  118. static const struct i2c_device_id tmp401_id[] = {
  119. { "tmp401", tmp401 },
  120. { "tmp411", tmp411 },
  121. { "tmp431", tmp431 },
  122. { "tmp432", tmp432 },
  123. { "tmp435", tmp435 },
  124. { "tmp461", tmp461 },
  125. { }
  126. };
  127. MODULE_DEVICE_TABLE(i2c, tmp401_id);
  128. /*
  129. * Client data (each client gets its own)
  130. */
  131. struct tmp401_data {
  132. struct i2c_client *client;
  133. const struct attribute_group *groups[3];
  134. struct mutex update_lock;
  135. char valid; /* zero until following fields are valid */
  136. unsigned long last_updated; /* in jiffies */
  137. enum chips kind;
  138. unsigned int update_interval; /* in milliseconds */
  139. /* register values */
  140. u8 status[4];
  141. u8 config;
  142. u16 temp[7][3];
  143. u8 temp_crit_hyst;
  144. };
  145. /*
  146. * Sysfs attr show / store functions
  147. */
  148. static int tmp401_register_to_temp(u16 reg, u8 config)
  149. {
  150. int temp = reg;
  151. if (config & TMP401_CONFIG_RANGE)
  152. temp -= 64 * 256;
  153. return DIV_ROUND_CLOSEST(temp * 125, 32);
  154. }
  155. static u16 tmp401_temp_to_register(long temp, u8 config, int zbits)
  156. {
  157. if (config & TMP401_CONFIG_RANGE) {
  158. temp = clamp_val(temp, -64000, 191000);
  159. temp += 64000;
  160. } else
  161. temp = clamp_val(temp, 0, 127000);
  162. return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
  163. }
  164. static int tmp401_update_device_reg16(struct i2c_client *client,
  165. struct tmp401_data *data)
  166. {
  167. int i, j, val;
  168. int num_regs = data->kind == tmp411 ? 6 : 4;
  169. int num_sensors = data->kind == tmp432 ? 3 : 2;
  170. for (i = 0; i < num_sensors; i++) { /* local / r1 / r2 */
  171. for (j = 0; j < num_regs; j++) { /* temp / low / ... */
  172. u8 regaddr;
  173. regaddr = data->kind == tmp432 ?
  174. TMP432_TEMP_MSB_READ[j][i] :
  175. TMP401_TEMP_MSB_READ[j][i];
  176. if (j == 3) { /* crit is msb only */
  177. val = i2c_smbus_read_byte_data(client, regaddr);
  178. } else {
  179. val = i2c_smbus_read_word_swapped(client,
  180. regaddr);
  181. }
  182. if (val < 0)
  183. return val;
  184. data->temp[j][i] = j == 3 ? val << 8 : val;
  185. }
  186. }
  187. return 0;
  188. }
  189. static struct tmp401_data *tmp401_update_device(struct device *dev)
  190. {
  191. struct tmp401_data *data = dev_get_drvdata(dev);
  192. struct i2c_client *client = data->client;
  193. struct tmp401_data *ret = data;
  194. int i, val;
  195. unsigned long next_update;
  196. mutex_lock(&data->update_lock);
  197. next_update = data->last_updated +
  198. msecs_to_jiffies(data->update_interval);
  199. if (time_after(jiffies, next_update) || !data->valid) {
  200. if (data->kind != tmp432) {
  201. /*
  202. * The driver uses the TMP432 status format internally.
  203. * Convert status to TMP432 format for other chips.
  204. */
  205. val = i2c_smbus_read_byte_data(client, TMP401_STATUS);
  206. if (val < 0) {
  207. ret = ERR_PTR(val);
  208. goto abort;
  209. }
  210. data->status[0] =
  211. (val & TMP401_STATUS_REMOTE_OPEN) >> 1;
  212. data->status[1] =
  213. ((val & TMP401_STATUS_REMOTE_LOW) >> 2) |
  214. ((val & TMP401_STATUS_LOCAL_LOW) >> 5);
  215. data->status[2] =
  216. ((val & TMP401_STATUS_REMOTE_HIGH) >> 3) |
  217. ((val & TMP401_STATUS_LOCAL_HIGH) >> 6);
  218. data->status[3] = val & (TMP401_STATUS_LOCAL_CRIT
  219. | TMP401_STATUS_REMOTE_CRIT);
  220. } else {
  221. for (i = 0; i < ARRAY_SIZE(data->status); i++) {
  222. val = i2c_smbus_read_byte_data(client,
  223. TMP432_STATUS_REG[i]);
  224. if (val < 0) {
  225. ret = ERR_PTR(val);
  226. goto abort;
  227. }
  228. data->status[i] = val;
  229. }
  230. }
  231. val = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  232. if (val < 0) {
  233. ret = ERR_PTR(val);
  234. goto abort;
  235. }
  236. data->config = val;
  237. val = tmp401_update_device_reg16(client, data);
  238. if (val < 0) {
  239. ret = ERR_PTR(val);
  240. goto abort;
  241. }
  242. val = i2c_smbus_read_byte_data(client, TMP401_TEMP_CRIT_HYST);
  243. if (val < 0) {
  244. ret = ERR_PTR(val);
  245. goto abort;
  246. }
  247. data->temp_crit_hyst = val;
  248. data->last_updated = jiffies;
  249. data->valid = 1;
  250. }
  251. abort:
  252. mutex_unlock(&data->update_lock);
  253. return ret;
  254. }
  255. static ssize_t show_temp(struct device *dev,
  256. struct device_attribute *devattr, char *buf)
  257. {
  258. int nr = to_sensor_dev_attr_2(devattr)->nr;
  259. int index = to_sensor_dev_attr_2(devattr)->index;
  260. struct tmp401_data *data = tmp401_update_device(dev);
  261. if (IS_ERR(data))
  262. return PTR_ERR(data);
  263. return sprintf(buf, "%d\n",
  264. tmp401_register_to_temp(data->temp[nr][index], data->config));
  265. }
  266. static ssize_t show_temp_crit_hyst(struct device *dev,
  267. struct device_attribute *devattr, char *buf)
  268. {
  269. int temp, index = to_sensor_dev_attr(devattr)->index;
  270. struct tmp401_data *data = tmp401_update_device(dev);
  271. if (IS_ERR(data))
  272. return PTR_ERR(data);
  273. mutex_lock(&data->update_lock);
  274. temp = tmp401_register_to_temp(data->temp[3][index], data->config);
  275. temp -= data->temp_crit_hyst * 1000;
  276. mutex_unlock(&data->update_lock);
  277. return sprintf(buf, "%d\n", temp);
  278. }
  279. static ssize_t show_status(struct device *dev,
  280. struct device_attribute *devattr, char *buf)
  281. {
  282. int nr = to_sensor_dev_attr_2(devattr)->nr;
  283. int mask = to_sensor_dev_attr_2(devattr)->index;
  284. struct tmp401_data *data = tmp401_update_device(dev);
  285. if (IS_ERR(data))
  286. return PTR_ERR(data);
  287. return sprintf(buf, "%d\n", !!(data->status[nr] & mask));
  288. }
  289. static ssize_t store_temp(struct device *dev, struct device_attribute *devattr,
  290. const char *buf, size_t count)
  291. {
  292. int nr = to_sensor_dev_attr_2(devattr)->nr;
  293. int index = to_sensor_dev_attr_2(devattr)->index;
  294. struct tmp401_data *data = dev_get_drvdata(dev);
  295. struct i2c_client *client = data->client;
  296. long val;
  297. u16 reg;
  298. u8 regaddr;
  299. if (kstrtol(buf, 10, &val))
  300. return -EINVAL;
  301. reg = tmp401_temp_to_register(val, data->config, nr == 3 ? 8 : 4);
  302. mutex_lock(&data->update_lock);
  303. regaddr = data->kind == tmp432 ? TMP432_TEMP_MSB_WRITE[nr][index]
  304. : TMP401_TEMP_MSB_WRITE[nr][index];
  305. if (nr == 3) { /* crit is msb only */
  306. i2c_smbus_write_byte_data(client, regaddr, reg >> 8);
  307. } else {
  308. /* Hardware expects big endian data --> use _swapped */
  309. i2c_smbus_write_word_swapped(client, regaddr, reg);
  310. }
  311. data->temp[nr][index] = reg;
  312. mutex_unlock(&data->update_lock);
  313. return count;
  314. }
  315. static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
  316. *devattr, const char *buf, size_t count)
  317. {
  318. int temp, index = to_sensor_dev_attr(devattr)->index;
  319. struct tmp401_data *data = tmp401_update_device(dev);
  320. long val;
  321. u8 reg;
  322. if (IS_ERR(data))
  323. return PTR_ERR(data);
  324. if (kstrtol(buf, 10, &val))
  325. return -EINVAL;
  326. if (data->config & TMP401_CONFIG_RANGE)
  327. val = clamp_val(val, -64000, 191000);
  328. else
  329. val = clamp_val(val, 0, 127000);
  330. mutex_lock(&data->update_lock);
  331. temp = tmp401_register_to_temp(data->temp[3][index], data->config);
  332. val = clamp_val(val, temp - 255000, temp);
  333. reg = ((temp - val) + 500) / 1000;
  334. i2c_smbus_write_byte_data(data->client, TMP401_TEMP_CRIT_HYST,
  335. reg);
  336. data->temp_crit_hyst = reg;
  337. mutex_unlock(&data->update_lock);
  338. return count;
  339. }
  340. /*
  341. * Resets the historical measurements of minimum and maximum temperatures.
  342. * This is done by writing any value to any of the minimum/maximum registers
  343. * (0x30-0x37).
  344. */
  345. static ssize_t reset_temp_history(struct device *dev,
  346. struct device_attribute *devattr, const char *buf, size_t count)
  347. {
  348. struct tmp401_data *data = dev_get_drvdata(dev);
  349. struct i2c_client *client = data->client;
  350. long val;
  351. if (kstrtol(buf, 10, &val))
  352. return -EINVAL;
  353. if (val != 1) {
  354. dev_err(dev,
  355. "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
  356. val);
  357. return -EINVAL;
  358. }
  359. mutex_lock(&data->update_lock);
  360. i2c_smbus_write_byte_data(client, TMP401_TEMP_MSB_WRITE[5][0], val);
  361. data->valid = 0;
  362. mutex_unlock(&data->update_lock);
  363. return count;
  364. }
  365. static ssize_t update_interval_show(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. struct tmp401_data *data = dev_get_drvdata(dev);
  369. return sprintf(buf, "%u\n", data->update_interval);
  370. }
  371. static ssize_t update_interval_store(struct device *dev,
  372. struct device_attribute *attr,
  373. const char *buf, size_t count)
  374. {
  375. struct tmp401_data *data = dev_get_drvdata(dev);
  376. struct i2c_client *client = data->client;
  377. unsigned long val;
  378. int err, rate;
  379. err = kstrtoul(buf, 10, &val);
  380. if (err)
  381. return err;
  382. /*
  383. * For valid rates, interval can be calculated as
  384. * interval = (1 << (7 - rate)) * 125;
  385. * Rounded rate is therefore
  386. * rate = 7 - __fls(interval * 4 / (125 * 3));
  387. * Use clamp_val() to avoid overflows, and to ensure valid input
  388. * for __fls.
  389. */
  390. val = clamp_val(val, 125, 16000);
  391. rate = 7 - __fls(val * 4 / (125 * 3));
  392. mutex_lock(&data->update_lock);
  393. i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate);
  394. data->update_interval = (1 << (7 - rate)) * 125;
  395. mutex_unlock(&data->update_lock);
  396. return count;
  397. }
  398. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
  399. static SENSOR_DEVICE_ATTR_2(temp1_min, S_IWUSR | S_IRUGO, show_temp,
  400. store_temp, 1, 0);
  401. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IWUSR | S_IRUGO, show_temp,
  402. store_temp, 2, 0);
  403. static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IWUSR | S_IRUGO, show_temp,
  404. store_temp, 3, 0);
  405. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
  406. show_temp_crit_hyst, store_temp_crit_hyst, 0);
  407. static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_status, NULL,
  408. 1, TMP432_STATUS_LOCAL);
  409. static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_status, NULL,
  410. 2, TMP432_STATUS_LOCAL);
  411. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_status, NULL,
  412. 3, TMP432_STATUS_LOCAL);
  413. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
  414. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp,
  415. store_temp, 1, 1);
  416. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp,
  417. store_temp, 2, 1);
  418. static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IWUSR | S_IRUGO, show_temp,
  419. store_temp, 3, 1);
  420. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst,
  421. NULL, 1);
  422. static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_status, NULL,
  423. 0, TMP432_STATUS_REMOTE1);
  424. static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_status, NULL,
  425. 1, TMP432_STATUS_REMOTE1);
  426. static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_status, NULL,
  427. 2, TMP432_STATUS_REMOTE1);
  428. static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_status, NULL,
  429. 3, TMP432_STATUS_REMOTE1);
  430. static DEVICE_ATTR_RW(update_interval);
  431. static struct attribute *tmp401_attributes[] = {
  432. &sensor_dev_attr_temp1_input.dev_attr.attr,
  433. &sensor_dev_attr_temp1_min.dev_attr.attr,
  434. &sensor_dev_attr_temp1_max.dev_attr.attr,
  435. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  436. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  437. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  438. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  439. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  440. &sensor_dev_attr_temp2_input.dev_attr.attr,
  441. &sensor_dev_attr_temp2_min.dev_attr.attr,
  442. &sensor_dev_attr_temp2_max.dev_attr.attr,
  443. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  444. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  445. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  446. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  447. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  448. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  449. &dev_attr_update_interval.attr,
  450. NULL
  451. };
  452. static const struct attribute_group tmp401_group = {
  453. .attrs = tmp401_attributes,
  454. };
  455. /*
  456. * Additional features of the TMP411 chip.
  457. * The TMP411 stores the minimum and maximum
  458. * temperature measured since power-on, chip-reset, or
  459. * minimum and maximum register reset for both the local
  460. * and remote channels.
  461. */
  462. static SENSOR_DEVICE_ATTR_2(temp1_lowest, S_IRUGO, show_temp, NULL, 4, 0);
  463. static SENSOR_DEVICE_ATTR_2(temp1_highest, S_IRUGO, show_temp, NULL, 5, 0);
  464. static SENSOR_DEVICE_ATTR_2(temp2_lowest, S_IRUGO, show_temp, NULL, 4, 1);
  465. static SENSOR_DEVICE_ATTR_2(temp2_highest, S_IRUGO, show_temp, NULL, 5, 1);
  466. static SENSOR_DEVICE_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history,
  467. 0);
  468. static struct attribute *tmp411_attributes[] = {
  469. &sensor_dev_attr_temp1_highest.dev_attr.attr,
  470. &sensor_dev_attr_temp1_lowest.dev_attr.attr,
  471. &sensor_dev_attr_temp2_highest.dev_attr.attr,
  472. &sensor_dev_attr_temp2_lowest.dev_attr.attr,
  473. &sensor_dev_attr_temp_reset_history.dev_attr.attr,
  474. NULL
  475. };
  476. static const struct attribute_group tmp411_group = {
  477. .attrs = tmp411_attributes,
  478. };
  479. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2);
  480. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp,
  481. store_temp, 1, 2);
  482. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp,
  483. store_temp, 2, 2);
  484. static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
  485. store_temp, 3, 2);
  486. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst,
  487. NULL, 2);
  488. static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_status, NULL,
  489. 0, TMP432_STATUS_REMOTE2);
  490. static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_status, NULL,
  491. 1, TMP432_STATUS_REMOTE2);
  492. static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_status, NULL,
  493. 2, TMP432_STATUS_REMOTE2);
  494. static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_status, NULL,
  495. 3, TMP432_STATUS_REMOTE2);
  496. static struct attribute *tmp432_attributes[] = {
  497. &sensor_dev_attr_temp3_input.dev_attr.attr,
  498. &sensor_dev_attr_temp3_min.dev_attr.attr,
  499. &sensor_dev_attr_temp3_max.dev_attr.attr,
  500. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  501. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  502. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  503. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  504. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  505. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  506. NULL
  507. };
  508. static const struct attribute_group tmp432_group = {
  509. .attrs = tmp432_attributes,
  510. };
  511. /*
  512. * Additional features of the TMP461 chip.
  513. * The TMP461 temperature offset for the remote channel.
  514. */
  515. static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp,
  516. store_temp, 6, 1);
  517. static struct attribute *tmp461_attributes[] = {
  518. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  519. NULL
  520. };
  521. static const struct attribute_group tmp461_group = {
  522. .attrs = tmp461_attributes,
  523. };
  524. /*
  525. * Begin non sysfs callback code (aka Real code)
  526. */
  527. static int tmp401_init_client(struct tmp401_data *data,
  528. struct i2c_client *client)
  529. {
  530. int config, config_orig, status = 0;
  531. /* Set the conversion rate to 2 Hz */
  532. i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
  533. data->update_interval = 500;
  534. /* Start conversions (disable shutdown if necessary) */
  535. config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  536. if (config < 0)
  537. return config;
  538. config_orig = config;
  539. config &= ~TMP401_CONFIG_SHUTDOWN;
  540. if (config != config_orig)
  541. status = i2c_smbus_write_byte_data(client,
  542. TMP401_CONFIG_WRITE,
  543. config);
  544. return status;
  545. }
  546. static int tmp401_detect(struct i2c_client *client,
  547. struct i2c_board_info *info)
  548. {
  549. enum chips kind;
  550. struct i2c_adapter *adapter = client->adapter;
  551. u8 reg;
  552. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  553. return -ENODEV;
  554. /* Detect and identify the chip */
  555. reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
  556. if (reg != TMP401_MANUFACTURER_ID)
  557. return -ENODEV;
  558. reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
  559. switch (reg) {
  560. case TMP401_DEVICE_ID:
  561. if (client->addr != 0x4c)
  562. return -ENODEV;
  563. kind = tmp401;
  564. break;
  565. case TMP411A_DEVICE_ID:
  566. if (client->addr != 0x4c)
  567. return -ENODEV;
  568. kind = tmp411;
  569. break;
  570. case TMP411B_DEVICE_ID:
  571. if (client->addr != 0x4d)
  572. return -ENODEV;
  573. kind = tmp411;
  574. break;
  575. case TMP411C_DEVICE_ID:
  576. if (client->addr != 0x4e)
  577. return -ENODEV;
  578. kind = tmp411;
  579. break;
  580. case TMP431_DEVICE_ID:
  581. if (client->addr != 0x4c && client->addr != 0x4d)
  582. return -ENODEV;
  583. kind = tmp431;
  584. break;
  585. case TMP432_DEVICE_ID:
  586. if (client->addr != 0x4c && client->addr != 0x4d)
  587. return -ENODEV;
  588. kind = tmp432;
  589. break;
  590. case TMP435_DEVICE_ID:
  591. kind = tmp435;
  592. break;
  593. default:
  594. return -ENODEV;
  595. }
  596. reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  597. if (reg & 0x1b)
  598. return -ENODEV;
  599. reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
  600. /* Datasheet says: 0x1-0x6 */
  601. if (reg > 15)
  602. return -ENODEV;
  603. strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
  604. return 0;
  605. }
  606. static int tmp401_probe(struct i2c_client *client,
  607. const struct i2c_device_id *id)
  608. {
  609. static const char * const names[] = {
  610. "TMP401", "TMP411", "TMP431", "TMP432", "TMP435", "TMP461"
  611. };
  612. struct device *dev = &client->dev;
  613. struct device *hwmon_dev;
  614. struct tmp401_data *data;
  615. int groups = 0, status;
  616. data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL);
  617. if (!data)
  618. return -ENOMEM;
  619. data->client = client;
  620. mutex_init(&data->update_lock);
  621. data->kind = id->driver_data;
  622. /* Initialize the TMP401 chip */
  623. status = tmp401_init_client(data, client);
  624. if (status < 0)
  625. return status;
  626. /* Register sysfs hooks */
  627. data->groups[groups++] = &tmp401_group;
  628. /* Register additional tmp411 sysfs hooks */
  629. if (data->kind == tmp411)
  630. data->groups[groups++] = &tmp411_group;
  631. /* Register additional tmp432 sysfs hooks */
  632. if (data->kind == tmp432)
  633. data->groups[groups++] = &tmp432_group;
  634. if (data->kind == tmp461)
  635. data->groups[groups++] = &tmp461_group;
  636. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  637. data, data->groups);
  638. if (IS_ERR(hwmon_dev))
  639. return PTR_ERR(hwmon_dev);
  640. dev_info(dev, "Detected TI %s chip\n", names[data->kind]);
  641. return 0;
  642. }
  643. static struct i2c_driver tmp401_driver = {
  644. .class = I2C_CLASS_HWMON,
  645. .driver = {
  646. .name = "tmp401",
  647. },
  648. .probe = tmp401_probe,
  649. .id_table = tmp401_id,
  650. .detect = tmp401_detect,
  651. .address_list = normal_i2c,
  652. };
  653. module_i2c_driver(tmp401_driver);
  654. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  655. MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
  656. MODULE_LICENSE("GPL");