smm665.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Driver for SMM665 Power Controller / Monitor
  3. *
  4. * Copyright (C) 2010 Ericsson AB.
  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 as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This driver should also work for SMM465, SMM764, and SMM766, but is untested
  11. * for those chips. Only monitoring functionality is implemented.
  12. *
  13. * Datasheets:
  14. * http://www.summitmicro.com/prod_select/summary/SMM665/SMM665B_2089_20.pdf
  15. * http://www.summitmicro.com/prod_select/summary/SMM766B/SMM766B_2122.pdf
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/hwmon-sysfs.h>
  25. #include <linux/delay.h>
  26. #include <linux/jiffies.h>
  27. /* Internal reference voltage (VREF, x 1000 */
  28. #define SMM665_VREF_ADC_X1000 1250
  29. /* module parameters */
  30. static int vref = SMM665_VREF_ADC_X1000;
  31. module_param(vref, int, 0);
  32. MODULE_PARM_DESC(vref, "Reference voltage in mV");
  33. enum chips { smm465, smm665, smm665c, smm764, smm766 };
  34. /*
  35. * ADC channel addresses
  36. */
  37. #define SMM665_MISC16_ADC_DATA_A 0x00
  38. #define SMM665_MISC16_ADC_DATA_B 0x01
  39. #define SMM665_MISC16_ADC_DATA_C 0x02
  40. #define SMM665_MISC16_ADC_DATA_D 0x03
  41. #define SMM665_MISC16_ADC_DATA_E 0x04
  42. #define SMM665_MISC16_ADC_DATA_F 0x05
  43. #define SMM665_MISC16_ADC_DATA_VDD 0x06
  44. #define SMM665_MISC16_ADC_DATA_12V 0x07
  45. #define SMM665_MISC16_ADC_DATA_INT_TEMP 0x08
  46. #define SMM665_MISC16_ADC_DATA_AIN1 0x09
  47. #define SMM665_MISC16_ADC_DATA_AIN2 0x0a
  48. /*
  49. * Command registers
  50. */
  51. #define SMM665_MISC8_CMD_STS 0x80
  52. #define SMM665_MISC8_STATUS1 0x81
  53. #define SMM665_MISC8_STATUSS2 0x82
  54. #define SMM665_MISC8_IO_POLARITY 0x83
  55. #define SMM665_MISC8_PUP_POLARITY 0x84
  56. #define SMM665_MISC8_ADOC_STATUS1 0x85
  57. #define SMM665_MISC8_ADOC_STATUS2 0x86
  58. #define SMM665_MISC8_WRITE_PROT 0x87
  59. #define SMM665_MISC8_STS_TRACK 0x88
  60. /*
  61. * Configuration registers and register groups
  62. */
  63. #define SMM665_ADOC_ENABLE 0x0d
  64. #define SMM665_LIMIT_BASE 0x80 /* First limit register */
  65. /*
  66. * Limit register bit masks
  67. */
  68. #define SMM665_TRIGGER_RST 0x8000
  69. #define SMM665_TRIGGER_HEALTHY 0x4000
  70. #define SMM665_TRIGGER_POWEROFF 0x2000
  71. #define SMM665_TRIGGER_SHUTDOWN 0x1000
  72. #define SMM665_ADC_MASK 0x03ff
  73. #define smm665_is_critical(lim) ((lim) & (SMM665_TRIGGER_RST \
  74. | SMM665_TRIGGER_POWEROFF \
  75. | SMM665_TRIGGER_SHUTDOWN))
  76. /*
  77. * Fault register bit definitions
  78. * Values are merged from status registers 1/2,
  79. * with status register 1 providing the upper 8 bits.
  80. */
  81. #define SMM665_FAULT_A 0x0001
  82. #define SMM665_FAULT_B 0x0002
  83. #define SMM665_FAULT_C 0x0004
  84. #define SMM665_FAULT_D 0x0008
  85. #define SMM665_FAULT_E 0x0010
  86. #define SMM665_FAULT_F 0x0020
  87. #define SMM665_FAULT_VDD 0x0040
  88. #define SMM665_FAULT_12V 0x0080
  89. #define SMM665_FAULT_TEMP 0x0100
  90. #define SMM665_FAULT_AIN1 0x0200
  91. #define SMM665_FAULT_AIN2 0x0400
  92. /*
  93. * I2C Register addresses
  94. *
  95. * The configuration register needs to be the configured base register.
  96. * The command/status register address is derived from it.
  97. */
  98. #define SMM665_REGMASK 0x78
  99. #define SMM665_CMDREG_BASE 0x48
  100. #define SMM665_CONFREG_BASE 0x50
  101. /*
  102. * Equations given by chip manufacturer to calculate voltage/temperature values
  103. * vref = Reference voltage on VREF_ADC pin (module parameter)
  104. * adc = 10bit ADC value read back from registers
  105. */
  106. /* Voltage A-F and VDD */
  107. #define SMM665_VMON_ADC_TO_VOLTS(adc) ((adc) * vref / 256)
  108. /* Voltage 12VIN */
  109. #define SMM665_12VIN_ADC_TO_VOLTS(adc) ((adc) * vref * 3 / 256)
  110. /* Voltage AIN1, AIN2 */
  111. #define SMM665_AIN_ADC_TO_VOLTS(adc) ((adc) * vref / 512)
  112. /* Temp Sensor */
  113. #define SMM665_TEMP_ADC_TO_CELSIUS(adc) (((adc) <= 511) ? \
  114. ((int)(adc) * 1000 / 4) : \
  115. (((int)(adc) - 0x400) * 1000 / 4))
  116. #define SMM665_NUM_ADC 11
  117. /*
  118. * Chip dependent ADC conversion time, in uS
  119. */
  120. #define SMM665_ADC_WAIT_SMM665 70
  121. #define SMM665_ADC_WAIT_SMM766 185
  122. struct smm665_data {
  123. enum chips type;
  124. int conversion_time; /* ADC conversion time */
  125. struct i2c_client *client;
  126. struct mutex update_lock;
  127. bool valid;
  128. unsigned long last_updated; /* in jiffies */
  129. u16 adc[SMM665_NUM_ADC]; /* adc values (raw) */
  130. u16 faults; /* fault status */
  131. /* The following values are in mV */
  132. int critical_min_limit[SMM665_NUM_ADC];
  133. int alarm_min_limit[SMM665_NUM_ADC];
  134. int critical_max_limit[SMM665_NUM_ADC];
  135. int alarm_max_limit[SMM665_NUM_ADC];
  136. struct i2c_client *cmdreg;
  137. };
  138. /*
  139. * smm665_read16()
  140. *
  141. * Read 16 bit value from <reg>, <reg+1>. Upper 8 bits are in <reg>.
  142. */
  143. static int smm665_read16(struct i2c_client *client, int reg)
  144. {
  145. int rv, val;
  146. rv = i2c_smbus_read_byte_data(client, reg);
  147. if (rv < 0)
  148. return rv;
  149. val = rv << 8;
  150. rv = i2c_smbus_read_byte_data(client, reg + 1);
  151. if (rv < 0)
  152. return rv;
  153. val |= rv;
  154. return val;
  155. }
  156. /*
  157. * Read adc value.
  158. */
  159. static int smm665_read_adc(struct smm665_data *data, int adc)
  160. {
  161. struct i2c_client *client = data->cmdreg;
  162. int rv;
  163. int radc;
  164. /*
  165. * Algorithm for reading ADC, per SMM665 datasheet
  166. *
  167. * {[S][addr][W][Ack]} {[offset][Ack]} {[S][addr][R][Nack]}
  168. * [wait conversion time]
  169. * {[S][addr][R][Ack]} {[datahi][Ack]} {[datalo][Ack][P]}
  170. *
  171. * To implement the first part of this exchange,
  172. * do a full read transaction and expect a failure/Nack.
  173. * This sets up the address pointer on the SMM665
  174. * and starts the ADC conversion.
  175. * Then do a two-byte read transaction.
  176. */
  177. rv = i2c_smbus_read_byte_data(client, adc << 3);
  178. if (rv != -ENXIO) {
  179. /*
  180. * We expect ENXIO to reflect NACK
  181. * (per Documentation/i2c/fault-codes).
  182. * Everything else is an error.
  183. */
  184. dev_dbg(&client->dev,
  185. "Unexpected return code %d when setting ADC index", rv);
  186. return (rv < 0) ? rv : -EIO;
  187. }
  188. udelay(data->conversion_time);
  189. /*
  190. * Now read two bytes.
  191. *
  192. * Neither i2c_smbus_read_byte() nor
  193. * i2c_smbus_read_block_data() worked here,
  194. * so use i2c_smbus_read_word_swapped() instead.
  195. * We could also try to use i2c_master_recv(),
  196. * but that is not always supported.
  197. */
  198. rv = i2c_smbus_read_word_swapped(client, 0);
  199. if (rv < 0) {
  200. dev_dbg(&client->dev, "Failed to read ADC value: error %d", rv);
  201. return rv;
  202. }
  203. /*
  204. * Validate/verify readback adc channel (in bit 11..14).
  205. */
  206. radc = (rv >> 11) & 0x0f;
  207. if (radc != adc) {
  208. dev_dbg(&client->dev, "Unexpected RADC: Expected %d got %d",
  209. adc, radc);
  210. return -EIO;
  211. }
  212. return rv & SMM665_ADC_MASK;
  213. }
  214. static struct smm665_data *smm665_update_device(struct device *dev)
  215. {
  216. struct smm665_data *data = dev_get_drvdata(dev);
  217. struct i2c_client *client = data->client;
  218. struct smm665_data *ret = data;
  219. mutex_lock(&data->update_lock);
  220. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  221. int i, val;
  222. /*
  223. * read status registers
  224. */
  225. val = smm665_read16(client, SMM665_MISC8_STATUS1);
  226. if (unlikely(val < 0)) {
  227. ret = ERR_PTR(val);
  228. goto abort;
  229. }
  230. data->faults = val;
  231. /* Read adc registers */
  232. for (i = 0; i < SMM665_NUM_ADC; i++) {
  233. val = smm665_read_adc(data, i);
  234. if (unlikely(val < 0)) {
  235. ret = ERR_PTR(val);
  236. goto abort;
  237. }
  238. data->adc[i] = val;
  239. }
  240. data->last_updated = jiffies;
  241. data->valid = 1;
  242. }
  243. abort:
  244. mutex_unlock(&data->update_lock);
  245. return ret;
  246. }
  247. /* Return converted value from given adc */
  248. static int smm665_convert(u16 adcval, int index)
  249. {
  250. int val = 0;
  251. switch (index) {
  252. case SMM665_MISC16_ADC_DATA_12V:
  253. val = SMM665_12VIN_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  254. break;
  255. case SMM665_MISC16_ADC_DATA_VDD:
  256. case SMM665_MISC16_ADC_DATA_A:
  257. case SMM665_MISC16_ADC_DATA_B:
  258. case SMM665_MISC16_ADC_DATA_C:
  259. case SMM665_MISC16_ADC_DATA_D:
  260. case SMM665_MISC16_ADC_DATA_E:
  261. case SMM665_MISC16_ADC_DATA_F:
  262. val = SMM665_VMON_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  263. break;
  264. case SMM665_MISC16_ADC_DATA_AIN1:
  265. case SMM665_MISC16_ADC_DATA_AIN2:
  266. val = SMM665_AIN_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  267. break;
  268. case SMM665_MISC16_ADC_DATA_INT_TEMP:
  269. val = SMM665_TEMP_ADC_TO_CELSIUS(adcval & SMM665_ADC_MASK);
  270. break;
  271. default:
  272. /* If we get here, the developer messed up */
  273. WARN_ON_ONCE(1);
  274. break;
  275. }
  276. return val;
  277. }
  278. static int smm665_get_min(struct device *dev, int index)
  279. {
  280. struct smm665_data *data = dev_get_drvdata(dev);
  281. return data->alarm_min_limit[index];
  282. }
  283. static int smm665_get_max(struct device *dev, int index)
  284. {
  285. struct smm665_data *data = dev_get_drvdata(dev);
  286. return data->alarm_max_limit[index];
  287. }
  288. static int smm665_get_lcrit(struct device *dev, int index)
  289. {
  290. struct smm665_data *data = dev_get_drvdata(dev);
  291. return data->critical_min_limit[index];
  292. }
  293. static int smm665_get_crit(struct device *dev, int index)
  294. {
  295. struct smm665_data *data = dev_get_drvdata(dev);
  296. return data->critical_max_limit[index];
  297. }
  298. static ssize_t smm665_show_crit_alarm(struct device *dev,
  299. struct device_attribute *da, char *buf)
  300. {
  301. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  302. struct smm665_data *data = smm665_update_device(dev);
  303. int val = 0;
  304. if (IS_ERR(data))
  305. return PTR_ERR(data);
  306. if (data->faults & (1 << attr->index))
  307. val = 1;
  308. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  309. }
  310. static ssize_t smm665_show_input(struct device *dev,
  311. struct device_attribute *da, char *buf)
  312. {
  313. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  314. struct smm665_data *data = smm665_update_device(dev);
  315. int adc = attr->index;
  316. int val;
  317. if (IS_ERR(data))
  318. return PTR_ERR(data);
  319. val = smm665_convert(data->adc[adc], adc);
  320. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  321. }
  322. #define SMM665_SHOW(what) \
  323. static ssize_t smm665_show_##what(struct device *dev, \
  324. struct device_attribute *da, char *buf) \
  325. { \
  326. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  327. const int val = smm665_get_##what(dev, attr->index); \
  328. return snprintf(buf, PAGE_SIZE, "%d\n", val); \
  329. }
  330. SMM665_SHOW(min);
  331. SMM665_SHOW(max);
  332. SMM665_SHOW(lcrit);
  333. SMM665_SHOW(crit);
  334. /*
  335. * These macros are used below in constructing device attribute objects
  336. * for use with sysfs_create_group() to make a sysfs device file
  337. * for each register.
  338. */
  339. #define SMM665_ATTR(name, type, cmd_idx) \
  340. static SENSOR_DEVICE_ATTR(name##_##type, S_IRUGO, \
  341. smm665_show_##type, NULL, cmd_idx)
  342. /* Construct a sensor_device_attribute structure for each register */
  343. /* Input voltages */
  344. SMM665_ATTR(in1, input, SMM665_MISC16_ADC_DATA_12V);
  345. SMM665_ATTR(in2, input, SMM665_MISC16_ADC_DATA_VDD);
  346. SMM665_ATTR(in3, input, SMM665_MISC16_ADC_DATA_A);
  347. SMM665_ATTR(in4, input, SMM665_MISC16_ADC_DATA_B);
  348. SMM665_ATTR(in5, input, SMM665_MISC16_ADC_DATA_C);
  349. SMM665_ATTR(in6, input, SMM665_MISC16_ADC_DATA_D);
  350. SMM665_ATTR(in7, input, SMM665_MISC16_ADC_DATA_E);
  351. SMM665_ATTR(in8, input, SMM665_MISC16_ADC_DATA_F);
  352. SMM665_ATTR(in9, input, SMM665_MISC16_ADC_DATA_AIN1);
  353. SMM665_ATTR(in10, input, SMM665_MISC16_ADC_DATA_AIN2);
  354. /* Input voltages min */
  355. SMM665_ATTR(in1, min, SMM665_MISC16_ADC_DATA_12V);
  356. SMM665_ATTR(in2, min, SMM665_MISC16_ADC_DATA_VDD);
  357. SMM665_ATTR(in3, min, SMM665_MISC16_ADC_DATA_A);
  358. SMM665_ATTR(in4, min, SMM665_MISC16_ADC_DATA_B);
  359. SMM665_ATTR(in5, min, SMM665_MISC16_ADC_DATA_C);
  360. SMM665_ATTR(in6, min, SMM665_MISC16_ADC_DATA_D);
  361. SMM665_ATTR(in7, min, SMM665_MISC16_ADC_DATA_E);
  362. SMM665_ATTR(in8, min, SMM665_MISC16_ADC_DATA_F);
  363. SMM665_ATTR(in9, min, SMM665_MISC16_ADC_DATA_AIN1);
  364. SMM665_ATTR(in10, min, SMM665_MISC16_ADC_DATA_AIN2);
  365. /* Input voltages max */
  366. SMM665_ATTR(in1, max, SMM665_MISC16_ADC_DATA_12V);
  367. SMM665_ATTR(in2, max, SMM665_MISC16_ADC_DATA_VDD);
  368. SMM665_ATTR(in3, max, SMM665_MISC16_ADC_DATA_A);
  369. SMM665_ATTR(in4, max, SMM665_MISC16_ADC_DATA_B);
  370. SMM665_ATTR(in5, max, SMM665_MISC16_ADC_DATA_C);
  371. SMM665_ATTR(in6, max, SMM665_MISC16_ADC_DATA_D);
  372. SMM665_ATTR(in7, max, SMM665_MISC16_ADC_DATA_E);
  373. SMM665_ATTR(in8, max, SMM665_MISC16_ADC_DATA_F);
  374. SMM665_ATTR(in9, max, SMM665_MISC16_ADC_DATA_AIN1);
  375. SMM665_ATTR(in10, max, SMM665_MISC16_ADC_DATA_AIN2);
  376. /* Input voltages lcrit */
  377. SMM665_ATTR(in1, lcrit, SMM665_MISC16_ADC_DATA_12V);
  378. SMM665_ATTR(in2, lcrit, SMM665_MISC16_ADC_DATA_VDD);
  379. SMM665_ATTR(in3, lcrit, SMM665_MISC16_ADC_DATA_A);
  380. SMM665_ATTR(in4, lcrit, SMM665_MISC16_ADC_DATA_B);
  381. SMM665_ATTR(in5, lcrit, SMM665_MISC16_ADC_DATA_C);
  382. SMM665_ATTR(in6, lcrit, SMM665_MISC16_ADC_DATA_D);
  383. SMM665_ATTR(in7, lcrit, SMM665_MISC16_ADC_DATA_E);
  384. SMM665_ATTR(in8, lcrit, SMM665_MISC16_ADC_DATA_F);
  385. SMM665_ATTR(in9, lcrit, SMM665_MISC16_ADC_DATA_AIN1);
  386. SMM665_ATTR(in10, lcrit, SMM665_MISC16_ADC_DATA_AIN2);
  387. /* Input voltages crit */
  388. SMM665_ATTR(in1, crit, SMM665_MISC16_ADC_DATA_12V);
  389. SMM665_ATTR(in2, crit, SMM665_MISC16_ADC_DATA_VDD);
  390. SMM665_ATTR(in3, crit, SMM665_MISC16_ADC_DATA_A);
  391. SMM665_ATTR(in4, crit, SMM665_MISC16_ADC_DATA_B);
  392. SMM665_ATTR(in5, crit, SMM665_MISC16_ADC_DATA_C);
  393. SMM665_ATTR(in6, crit, SMM665_MISC16_ADC_DATA_D);
  394. SMM665_ATTR(in7, crit, SMM665_MISC16_ADC_DATA_E);
  395. SMM665_ATTR(in8, crit, SMM665_MISC16_ADC_DATA_F);
  396. SMM665_ATTR(in9, crit, SMM665_MISC16_ADC_DATA_AIN1);
  397. SMM665_ATTR(in10, crit, SMM665_MISC16_ADC_DATA_AIN2);
  398. /* critical alarms */
  399. SMM665_ATTR(in1, crit_alarm, SMM665_FAULT_12V);
  400. SMM665_ATTR(in2, crit_alarm, SMM665_FAULT_VDD);
  401. SMM665_ATTR(in3, crit_alarm, SMM665_FAULT_A);
  402. SMM665_ATTR(in4, crit_alarm, SMM665_FAULT_B);
  403. SMM665_ATTR(in5, crit_alarm, SMM665_FAULT_C);
  404. SMM665_ATTR(in6, crit_alarm, SMM665_FAULT_D);
  405. SMM665_ATTR(in7, crit_alarm, SMM665_FAULT_E);
  406. SMM665_ATTR(in8, crit_alarm, SMM665_FAULT_F);
  407. SMM665_ATTR(in9, crit_alarm, SMM665_FAULT_AIN1);
  408. SMM665_ATTR(in10, crit_alarm, SMM665_FAULT_AIN2);
  409. /* Temperature */
  410. SMM665_ATTR(temp1, input, SMM665_MISC16_ADC_DATA_INT_TEMP);
  411. SMM665_ATTR(temp1, min, SMM665_MISC16_ADC_DATA_INT_TEMP);
  412. SMM665_ATTR(temp1, max, SMM665_MISC16_ADC_DATA_INT_TEMP);
  413. SMM665_ATTR(temp1, lcrit, SMM665_MISC16_ADC_DATA_INT_TEMP);
  414. SMM665_ATTR(temp1, crit, SMM665_MISC16_ADC_DATA_INT_TEMP);
  415. SMM665_ATTR(temp1, crit_alarm, SMM665_FAULT_TEMP);
  416. /*
  417. * Finally, construct an array of pointers to members of the above objects,
  418. * as required for sysfs_create_group()
  419. */
  420. static struct attribute *smm665_attrs[] = {
  421. &sensor_dev_attr_in1_input.dev_attr.attr,
  422. &sensor_dev_attr_in1_min.dev_attr.attr,
  423. &sensor_dev_attr_in1_max.dev_attr.attr,
  424. &sensor_dev_attr_in1_lcrit.dev_attr.attr,
  425. &sensor_dev_attr_in1_crit.dev_attr.attr,
  426. &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
  427. &sensor_dev_attr_in2_input.dev_attr.attr,
  428. &sensor_dev_attr_in2_min.dev_attr.attr,
  429. &sensor_dev_attr_in2_max.dev_attr.attr,
  430. &sensor_dev_attr_in2_lcrit.dev_attr.attr,
  431. &sensor_dev_attr_in2_crit.dev_attr.attr,
  432. &sensor_dev_attr_in2_crit_alarm.dev_attr.attr,
  433. &sensor_dev_attr_in3_input.dev_attr.attr,
  434. &sensor_dev_attr_in3_min.dev_attr.attr,
  435. &sensor_dev_attr_in3_max.dev_attr.attr,
  436. &sensor_dev_attr_in3_lcrit.dev_attr.attr,
  437. &sensor_dev_attr_in3_crit.dev_attr.attr,
  438. &sensor_dev_attr_in3_crit_alarm.dev_attr.attr,
  439. &sensor_dev_attr_in4_input.dev_attr.attr,
  440. &sensor_dev_attr_in4_min.dev_attr.attr,
  441. &sensor_dev_attr_in4_max.dev_attr.attr,
  442. &sensor_dev_attr_in4_lcrit.dev_attr.attr,
  443. &sensor_dev_attr_in4_crit.dev_attr.attr,
  444. &sensor_dev_attr_in4_crit_alarm.dev_attr.attr,
  445. &sensor_dev_attr_in5_input.dev_attr.attr,
  446. &sensor_dev_attr_in5_min.dev_attr.attr,
  447. &sensor_dev_attr_in5_max.dev_attr.attr,
  448. &sensor_dev_attr_in5_lcrit.dev_attr.attr,
  449. &sensor_dev_attr_in5_crit.dev_attr.attr,
  450. &sensor_dev_attr_in5_crit_alarm.dev_attr.attr,
  451. &sensor_dev_attr_in6_input.dev_attr.attr,
  452. &sensor_dev_attr_in6_min.dev_attr.attr,
  453. &sensor_dev_attr_in6_max.dev_attr.attr,
  454. &sensor_dev_attr_in6_lcrit.dev_attr.attr,
  455. &sensor_dev_attr_in6_crit.dev_attr.attr,
  456. &sensor_dev_attr_in6_crit_alarm.dev_attr.attr,
  457. &sensor_dev_attr_in7_input.dev_attr.attr,
  458. &sensor_dev_attr_in7_min.dev_attr.attr,
  459. &sensor_dev_attr_in7_max.dev_attr.attr,
  460. &sensor_dev_attr_in7_lcrit.dev_attr.attr,
  461. &sensor_dev_attr_in7_crit.dev_attr.attr,
  462. &sensor_dev_attr_in7_crit_alarm.dev_attr.attr,
  463. &sensor_dev_attr_in8_input.dev_attr.attr,
  464. &sensor_dev_attr_in8_min.dev_attr.attr,
  465. &sensor_dev_attr_in8_max.dev_attr.attr,
  466. &sensor_dev_attr_in8_lcrit.dev_attr.attr,
  467. &sensor_dev_attr_in8_crit.dev_attr.attr,
  468. &sensor_dev_attr_in8_crit_alarm.dev_attr.attr,
  469. &sensor_dev_attr_in9_input.dev_attr.attr,
  470. &sensor_dev_attr_in9_min.dev_attr.attr,
  471. &sensor_dev_attr_in9_max.dev_attr.attr,
  472. &sensor_dev_attr_in9_lcrit.dev_attr.attr,
  473. &sensor_dev_attr_in9_crit.dev_attr.attr,
  474. &sensor_dev_attr_in9_crit_alarm.dev_attr.attr,
  475. &sensor_dev_attr_in10_input.dev_attr.attr,
  476. &sensor_dev_attr_in10_min.dev_attr.attr,
  477. &sensor_dev_attr_in10_max.dev_attr.attr,
  478. &sensor_dev_attr_in10_lcrit.dev_attr.attr,
  479. &sensor_dev_attr_in10_crit.dev_attr.attr,
  480. &sensor_dev_attr_in10_crit_alarm.dev_attr.attr,
  481. &sensor_dev_attr_temp1_input.dev_attr.attr,
  482. &sensor_dev_attr_temp1_min.dev_attr.attr,
  483. &sensor_dev_attr_temp1_max.dev_attr.attr,
  484. &sensor_dev_attr_temp1_lcrit.dev_attr.attr,
  485. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  486. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  487. NULL,
  488. };
  489. ATTRIBUTE_GROUPS(smm665);
  490. static int smm665_probe(struct i2c_client *client,
  491. const struct i2c_device_id *id)
  492. {
  493. struct i2c_adapter *adapter = client->adapter;
  494. struct smm665_data *data;
  495. struct device *hwmon_dev;
  496. int i, ret;
  497. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  498. | I2C_FUNC_SMBUS_WORD_DATA))
  499. return -ENODEV;
  500. if (i2c_smbus_read_byte_data(client, SMM665_ADOC_ENABLE) < 0)
  501. return -ENODEV;
  502. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  503. if (!data)
  504. return -ENOMEM;
  505. i2c_set_clientdata(client, data);
  506. mutex_init(&data->update_lock);
  507. data->client = client;
  508. data->type = id->driver_data;
  509. data->cmdreg = i2c_new_dummy(adapter, (client->addr & ~SMM665_REGMASK)
  510. | SMM665_CMDREG_BASE);
  511. if (!data->cmdreg)
  512. return -ENOMEM;
  513. switch (data->type) {
  514. case smm465:
  515. case smm665:
  516. data->conversion_time = SMM665_ADC_WAIT_SMM665;
  517. break;
  518. case smm665c:
  519. case smm764:
  520. case smm766:
  521. data->conversion_time = SMM665_ADC_WAIT_SMM766;
  522. break;
  523. }
  524. ret = -ENODEV;
  525. if (i2c_smbus_read_byte_data(data->cmdreg, SMM665_MISC8_CMD_STS) < 0)
  526. goto out_unregister;
  527. /*
  528. * Read limits.
  529. *
  530. * Limit registers start with register SMM665_LIMIT_BASE.
  531. * Each channel uses 8 registers, providing four limit values
  532. * per channel. Each limit value requires two registers, with the
  533. * high byte in the first register and the low byte in the second
  534. * register. The first two limits are under limit values, followed
  535. * by two over limit values.
  536. *
  537. * Limit register order matches the ADC register order, so we use
  538. * ADC register defines throughout the code to index limit registers.
  539. *
  540. * We save the first retrieved value both as "critical" and "alarm"
  541. * value. The second value overwrites either the critical or the
  542. * alarm value, depending on its configuration. This ensures that both
  543. * critical and alarm values are initialized, even if both registers are
  544. * configured as critical or non-critical.
  545. */
  546. for (i = 0; i < SMM665_NUM_ADC; i++) {
  547. int val;
  548. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8);
  549. if (unlikely(val < 0))
  550. goto out_unregister;
  551. data->critical_min_limit[i] = data->alarm_min_limit[i]
  552. = smm665_convert(val, i);
  553. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 2);
  554. if (unlikely(val < 0))
  555. goto out_unregister;
  556. if (smm665_is_critical(val))
  557. data->critical_min_limit[i] = smm665_convert(val, i);
  558. else
  559. data->alarm_min_limit[i] = smm665_convert(val, i);
  560. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 4);
  561. if (unlikely(val < 0))
  562. goto out_unregister;
  563. data->critical_max_limit[i] = data->alarm_max_limit[i]
  564. = smm665_convert(val, i);
  565. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 6);
  566. if (unlikely(val < 0))
  567. goto out_unregister;
  568. if (smm665_is_critical(val))
  569. data->critical_max_limit[i] = smm665_convert(val, i);
  570. else
  571. data->alarm_max_limit[i] = smm665_convert(val, i);
  572. }
  573. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  574. client->name, data,
  575. smm665_groups);
  576. if (IS_ERR(hwmon_dev)) {
  577. ret = PTR_ERR(hwmon_dev);
  578. goto out_unregister;
  579. }
  580. return 0;
  581. out_unregister:
  582. i2c_unregister_device(data->cmdreg);
  583. return ret;
  584. }
  585. static int smm665_remove(struct i2c_client *client)
  586. {
  587. struct smm665_data *data = i2c_get_clientdata(client);
  588. i2c_unregister_device(data->cmdreg);
  589. return 0;
  590. }
  591. static const struct i2c_device_id smm665_id[] = {
  592. {"smm465", smm465},
  593. {"smm665", smm665},
  594. {"smm665c", smm665c},
  595. {"smm764", smm764},
  596. {"smm766", smm766},
  597. {}
  598. };
  599. MODULE_DEVICE_TABLE(i2c, smm665_id);
  600. /* This is the driver that will be inserted */
  601. static struct i2c_driver smm665_driver = {
  602. .driver = {
  603. .name = "smm665",
  604. },
  605. .probe = smm665_probe,
  606. .remove = smm665_remove,
  607. .id_table = smm665_id,
  608. };
  609. module_i2c_driver(smm665_driver);
  610. MODULE_AUTHOR("Guenter Roeck");
  611. MODULE_DESCRIPTION("SMM665 driver");
  612. MODULE_LICENSE("GPL");