smm665.c 21 KB

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