smm665.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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_data() 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_data(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. * High byte is in lower 8 bit of rv, so only shift by 3.
  205. */
  206. radc = (rv >> 3) & 0x0f;
  207. if (radc != adc) {
  208. dev_dbg(&client->dev, "Unexpected RADC: Expected %d got %d",
  209. adc, radc);
  210. return -EIO;
  211. }
  212. /*
  213. * Chip replies with H/L, while SMBus expects L/H.
  214. * Thus, byte order is reversed, and we have to swap
  215. * the result.
  216. */
  217. rv = swab16(rv) & SMM665_ADC_MASK;
  218. return rv;
  219. }
  220. static struct smm665_data *smm665_update_device(struct device *dev)
  221. {
  222. struct i2c_client *client = to_i2c_client(dev);
  223. struct smm665_data *data = i2c_get_clientdata(client);
  224. struct smm665_data *ret = data;
  225. mutex_lock(&data->update_lock);
  226. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  227. int i, val;
  228. /*
  229. * read status registers
  230. */
  231. val = smm665_read16(client, SMM665_MISC8_STATUS1);
  232. if (unlikely(val < 0)) {
  233. ret = ERR_PTR(val);
  234. goto abort;
  235. }
  236. data->faults = val;
  237. /* Read adc registers */
  238. for (i = 0; i < SMM665_NUM_ADC; i++) {
  239. val = smm665_read_adc(data, i);
  240. if (unlikely(val < 0)) {
  241. ret = ERR_PTR(val);
  242. goto abort;
  243. }
  244. data->adc[i] = val;
  245. }
  246. data->last_updated = jiffies;
  247. data->valid = 1;
  248. }
  249. abort:
  250. mutex_unlock(&data->update_lock);
  251. return ret;
  252. }
  253. /* Return converted value from given adc */
  254. static int smm665_convert(u16 adcval, int index)
  255. {
  256. int val = 0;
  257. switch (index) {
  258. case SMM665_MISC16_ADC_DATA_12V:
  259. val = SMM665_12VIN_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  260. break;
  261. case SMM665_MISC16_ADC_DATA_VDD:
  262. case SMM665_MISC16_ADC_DATA_A:
  263. case SMM665_MISC16_ADC_DATA_B:
  264. case SMM665_MISC16_ADC_DATA_C:
  265. case SMM665_MISC16_ADC_DATA_D:
  266. case SMM665_MISC16_ADC_DATA_E:
  267. case SMM665_MISC16_ADC_DATA_F:
  268. val = SMM665_VMON_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  269. break;
  270. case SMM665_MISC16_ADC_DATA_AIN1:
  271. case SMM665_MISC16_ADC_DATA_AIN2:
  272. val = SMM665_AIN_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  273. break;
  274. case SMM665_MISC16_ADC_DATA_INT_TEMP:
  275. val = SMM665_TEMP_ADC_TO_CELSIUS(adcval & SMM665_ADC_MASK);
  276. break;
  277. default:
  278. /* If we get here, the developer messed up */
  279. WARN_ON_ONCE(1);
  280. break;
  281. }
  282. return val;
  283. }
  284. static int smm665_get_min(struct device *dev, int index)
  285. {
  286. struct i2c_client *client = to_i2c_client(dev);
  287. struct smm665_data *data = i2c_get_clientdata(client);
  288. return data->alarm_min_limit[index];
  289. }
  290. static int smm665_get_max(struct device *dev, int index)
  291. {
  292. struct i2c_client *client = to_i2c_client(dev);
  293. struct smm665_data *data = i2c_get_clientdata(client);
  294. return data->alarm_max_limit[index];
  295. }
  296. static int smm665_get_lcrit(struct device *dev, int index)
  297. {
  298. struct i2c_client *client = to_i2c_client(dev);
  299. struct smm665_data *data = i2c_get_clientdata(client);
  300. return data->critical_min_limit[index];
  301. }
  302. static int smm665_get_crit(struct device *dev, int index)
  303. {
  304. struct i2c_client *client = to_i2c_client(dev);
  305. struct smm665_data *data = i2c_get_clientdata(client);
  306. return data->critical_max_limit[index];
  307. }
  308. static ssize_t smm665_show_crit_alarm(struct device *dev,
  309. struct device_attribute *da, char *buf)
  310. {
  311. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  312. struct smm665_data *data = smm665_update_device(dev);
  313. int val = 0;
  314. if (IS_ERR(data))
  315. return PTR_ERR(data);
  316. if (data->faults & (1 << attr->index))
  317. val = 1;
  318. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  319. }
  320. static ssize_t smm665_show_input(struct device *dev,
  321. struct device_attribute *da, char *buf)
  322. {
  323. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  324. struct smm665_data *data = smm665_update_device(dev);
  325. int adc = attr->index;
  326. int val;
  327. if (IS_ERR(data))
  328. return PTR_ERR(data);
  329. val = smm665_convert(data->adc[adc], adc);
  330. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  331. }
  332. #define SMM665_SHOW(what) \
  333. static ssize_t smm665_show_##what(struct device *dev, \
  334. struct device_attribute *da, char *buf) \
  335. { \
  336. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  337. const int val = smm665_get_##what(dev, attr->index); \
  338. return snprintf(buf, PAGE_SIZE, "%d\n", val); \
  339. }
  340. SMM665_SHOW(min);
  341. SMM665_SHOW(max);
  342. SMM665_SHOW(lcrit);
  343. SMM665_SHOW(crit);
  344. /* These macros are used below in constructing device attribute objects
  345. * for use with sysfs_create_group() to make a sysfs device file
  346. * for each register.
  347. */
  348. #define SMM665_ATTR(name, type, cmd_idx) \
  349. static SENSOR_DEVICE_ATTR(name##_##type, S_IRUGO, \
  350. smm665_show_##type, NULL, cmd_idx)
  351. /* Construct a sensor_device_attribute structure for each register */
  352. /* Input voltages */
  353. SMM665_ATTR(in1, input, SMM665_MISC16_ADC_DATA_12V);
  354. SMM665_ATTR(in2, input, SMM665_MISC16_ADC_DATA_VDD);
  355. SMM665_ATTR(in3, input, SMM665_MISC16_ADC_DATA_A);
  356. SMM665_ATTR(in4, input, SMM665_MISC16_ADC_DATA_B);
  357. SMM665_ATTR(in5, input, SMM665_MISC16_ADC_DATA_C);
  358. SMM665_ATTR(in6, input, SMM665_MISC16_ADC_DATA_D);
  359. SMM665_ATTR(in7, input, SMM665_MISC16_ADC_DATA_E);
  360. SMM665_ATTR(in8, input, SMM665_MISC16_ADC_DATA_F);
  361. SMM665_ATTR(in9, input, SMM665_MISC16_ADC_DATA_AIN1);
  362. SMM665_ATTR(in10, input, SMM665_MISC16_ADC_DATA_AIN2);
  363. /* Input voltages min */
  364. SMM665_ATTR(in1, min, SMM665_MISC16_ADC_DATA_12V);
  365. SMM665_ATTR(in2, min, SMM665_MISC16_ADC_DATA_VDD);
  366. SMM665_ATTR(in3, min, SMM665_MISC16_ADC_DATA_A);
  367. SMM665_ATTR(in4, min, SMM665_MISC16_ADC_DATA_B);
  368. SMM665_ATTR(in5, min, SMM665_MISC16_ADC_DATA_C);
  369. SMM665_ATTR(in6, min, SMM665_MISC16_ADC_DATA_D);
  370. SMM665_ATTR(in7, min, SMM665_MISC16_ADC_DATA_E);
  371. SMM665_ATTR(in8, min, SMM665_MISC16_ADC_DATA_F);
  372. SMM665_ATTR(in9, min, SMM665_MISC16_ADC_DATA_AIN1);
  373. SMM665_ATTR(in10, min, SMM665_MISC16_ADC_DATA_AIN2);
  374. /* Input voltages max */
  375. SMM665_ATTR(in1, max, SMM665_MISC16_ADC_DATA_12V);
  376. SMM665_ATTR(in2, max, SMM665_MISC16_ADC_DATA_VDD);
  377. SMM665_ATTR(in3, max, SMM665_MISC16_ADC_DATA_A);
  378. SMM665_ATTR(in4, max, SMM665_MISC16_ADC_DATA_B);
  379. SMM665_ATTR(in5, max, SMM665_MISC16_ADC_DATA_C);
  380. SMM665_ATTR(in6, max, SMM665_MISC16_ADC_DATA_D);
  381. SMM665_ATTR(in7, max, SMM665_MISC16_ADC_DATA_E);
  382. SMM665_ATTR(in8, max, SMM665_MISC16_ADC_DATA_F);
  383. SMM665_ATTR(in9, max, SMM665_MISC16_ADC_DATA_AIN1);
  384. SMM665_ATTR(in10, max, SMM665_MISC16_ADC_DATA_AIN2);
  385. /* Input voltages lcrit */
  386. SMM665_ATTR(in1, lcrit, SMM665_MISC16_ADC_DATA_12V);
  387. SMM665_ATTR(in2, lcrit, SMM665_MISC16_ADC_DATA_VDD);
  388. SMM665_ATTR(in3, lcrit, SMM665_MISC16_ADC_DATA_A);
  389. SMM665_ATTR(in4, lcrit, SMM665_MISC16_ADC_DATA_B);
  390. SMM665_ATTR(in5, lcrit, SMM665_MISC16_ADC_DATA_C);
  391. SMM665_ATTR(in6, lcrit, SMM665_MISC16_ADC_DATA_D);
  392. SMM665_ATTR(in7, lcrit, SMM665_MISC16_ADC_DATA_E);
  393. SMM665_ATTR(in8, lcrit, SMM665_MISC16_ADC_DATA_F);
  394. SMM665_ATTR(in9, lcrit, SMM665_MISC16_ADC_DATA_AIN1);
  395. SMM665_ATTR(in10, lcrit, SMM665_MISC16_ADC_DATA_AIN2);
  396. /* Input voltages crit */
  397. SMM665_ATTR(in1, crit, SMM665_MISC16_ADC_DATA_12V);
  398. SMM665_ATTR(in2, crit, SMM665_MISC16_ADC_DATA_VDD);
  399. SMM665_ATTR(in3, crit, SMM665_MISC16_ADC_DATA_A);
  400. SMM665_ATTR(in4, crit, SMM665_MISC16_ADC_DATA_B);
  401. SMM665_ATTR(in5, crit, SMM665_MISC16_ADC_DATA_C);
  402. SMM665_ATTR(in6, crit, SMM665_MISC16_ADC_DATA_D);
  403. SMM665_ATTR(in7, crit, SMM665_MISC16_ADC_DATA_E);
  404. SMM665_ATTR(in8, crit, SMM665_MISC16_ADC_DATA_F);
  405. SMM665_ATTR(in9, crit, SMM665_MISC16_ADC_DATA_AIN1);
  406. SMM665_ATTR(in10, crit, SMM665_MISC16_ADC_DATA_AIN2);
  407. /* critical alarms */
  408. SMM665_ATTR(in1, crit_alarm, SMM665_FAULT_12V);
  409. SMM665_ATTR(in2, crit_alarm, SMM665_FAULT_VDD);
  410. SMM665_ATTR(in3, crit_alarm, SMM665_FAULT_A);
  411. SMM665_ATTR(in4, crit_alarm, SMM665_FAULT_B);
  412. SMM665_ATTR(in5, crit_alarm, SMM665_FAULT_C);
  413. SMM665_ATTR(in6, crit_alarm, SMM665_FAULT_D);
  414. SMM665_ATTR(in7, crit_alarm, SMM665_FAULT_E);
  415. SMM665_ATTR(in8, crit_alarm, SMM665_FAULT_F);
  416. SMM665_ATTR(in9, crit_alarm, SMM665_FAULT_AIN1);
  417. SMM665_ATTR(in10, crit_alarm, SMM665_FAULT_AIN2);
  418. /* Temperature */
  419. SMM665_ATTR(temp1, input, SMM665_MISC16_ADC_DATA_INT_TEMP);
  420. SMM665_ATTR(temp1, min, SMM665_MISC16_ADC_DATA_INT_TEMP);
  421. SMM665_ATTR(temp1, max, SMM665_MISC16_ADC_DATA_INT_TEMP);
  422. SMM665_ATTR(temp1, lcrit, SMM665_MISC16_ADC_DATA_INT_TEMP);
  423. SMM665_ATTR(temp1, crit, SMM665_MISC16_ADC_DATA_INT_TEMP);
  424. SMM665_ATTR(temp1, crit_alarm, SMM665_FAULT_TEMP);
  425. /*
  426. * Finally, construct an array of pointers to members of the above objects,
  427. * as required for sysfs_create_group()
  428. */
  429. static struct attribute *smm665_attributes[] = {
  430. &sensor_dev_attr_in1_input.dev_attr.attr,
  431. &sensor_dev_attr_in1_min.dev_attr.attr,
  432. &sensor_dev_attr_in1_max.dev_attr.attr,
  433. &sensor_dev_attr_in1_lcrit.dev_attr.attr,
  434. &sensor_dev_attr_in1_crit.dev_attr.attr,
  435. &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
  436. &sensor_dev_attr_in2_input.dev_attr.attr,
  437. &sensor_dev_attr_in2_min.dev_attr.attr,
  438. &sensor_dev_attr_in2_max.dev_attr.attr,
  439. &sensor_dev_attr_in2_lcrit.dev_attr.attr,
  440. &sensor_dev_attr_in2_crit.dev_attr.attr,
  441. &sensor_dev_attr_in2_crit_alarm.dev_attr.attr,
  442. &sensor_dev_attr_in3_input.dev_attr.attr,
  443. &sensor_dev_attr_in3_min.dev_attr.attr,
  444. &sensor_dev_attr_in3_max.dev_attr.attr,
  445. &sensor_dev_attr_in3_lcrit.dev_attr.attr,
  446. &sensor_dev_attr_in3_crit.dev_attr.attr,
  447. &sensor_dev_attr_in3_crit_alarm.dev_attr.attr,
  448. &sensor_dev_attr_in4_input.dev_attr.attr,
  449. &sensor_dev_attr_in4_min.dev_attr.attr,
  450. &sensor_dev_attr_in4_max.dev_attr.attr,
  451. &sensor_dev_attr_in4_lcrit.dev_attr.attr,
  452. &sensor_dev_attr_in4_crit.dev_attr.attr,
  453. &sensor_dev_attr_in4_crit_alarm.dev_attr.attr,
  454. &sensor_dev_attr_in5_input.dev_attr.attr,
  455. &sensor_dev_attr_in5_min.dev_attr.attr,
  456. &sensor_dev_attr_in5_max.dev_attr.attr,
  457. &sensor_dev_attr_in5_lcrit.dev_attr.attr,
  458. &sensor_dev_attr_in5_crit.dev_attr.attr,
  459. &sensor_dev_attr_in5_crit_alarm.dev_attr.attr,
  460. &sensor_dev_attr_in6_input.dev_attr.attr,
  461. &sensor_dev_attr_in6_min.dev_attr.attr,
  462. &sensor_dev_attr_in6_max.dev_attr.attr,
  463. &sensor_dev_attr_in6_lcrit.dev_attr.attr,
  464. &sensor_dev_attr_in6_crit.dev_attr.attr,
  465. &sensor_dev_attr_in6_crit_alarm.dev_attr.attr,
  466. &sensor_dev_attr_in7_input.dev_attr.attr,
  467. &sensor_dev_attr_in7_min.dev_attr.attr,
  468. &sensor_dev_attr_in7_max.dev_attr.attr,
  469. &sensor_dev_attr_in7_lcrit.dev_attr.attr,
  470. &sensor_dev_attr_in7_crit.dev_attr.attr,
  471. &sensor_dev_attr_in7_crit_alarm.dev_attr.attr,
  472. &sensor_dev_attr_in8_input.dev_attr.attr,
  473. &sensor_dev_attr_in8_min.dev_attr.attr,
  474. &sensor_dev_attr_in8_max.dev_attr.attr,
  475. &sensor_dev_attr_in8_lcrit.dev_attr.attr,
  476. &sensor_dev_attr_in8_crit.dev_attr.attr,
  477. &sensor_dev_attr_in8_crit_alarm.dev_attr.attr,
  478. &sensor_dev_attr_in9_input.dev_attr.attr,
  479. &sensor_dev_attr_in9_min.dev_attr.attr,
  480. &sensor_dev_attr_in9_max.dev_attr.attr,
  481. &sensor_dev_attr_in9_lcrit.dev_attr.attr,
  482. &sensor_dev_attr_in9_crit.dev_attr.attr,
  483. &sensor_dev_attr_in9_crit_alarm.dev_attr.attr,
  484. &sensor_dev_attr_in10_input.dev_attr.attr,
  485. &sensor_dev_attr_in10_min.dev_attr.attr,
  486. &sensor_dev_attr_in10_max.dev_attr.attr,
  487. &sensor_dev_attr_in10_lcrit.dev_attr.attr,
  488. &sensor_dev_attr_in10_crit.dev_attr.attr,
  489. &sensor_dev_attr_in10_crit_alarm.dev_attr.attr,
  490. &sensor_dev_attr_temp1_input.dev_attr.attr,
  491. &sensor_dev_attr_temp1_min.dev_attr.attr,
  492. &sensor_dev_attr_temp1_max.dev_attr.attr,
  493. &sensor_dev_attr_temp1_lcrit.dev_attr.attr,
  494. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  495. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  496. NULL,
  497. };
  498. static const struct attribute_group smm665_group = {
  499. .attrs = smm665_attributes,
  500. };
  501. static int smm665_probe(struct i2c_client *client,
  502. const struct i2c_device_id *id)
  503. {
  504. struct i2c_adapter *adapter = client->adapter;
  505. struct smm665_data *data;
  506. int i, ret;
  507. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  508. | I2C_FUNC_SMBUS_WORD_DATA))
  509. return -ENODEV;
  510. if (i2c_smbus_read_byte_data(client, SMM665_ADOC_ENABLE) < 0)
  511. return -ENODEV;
  512. ret = -ENOMEM;
  513. data = kzalloc(sizeof(*data), GFP_KERNEL);
  514. if (!data)
  515. goto out_return;
  516. i2c_set_clientdata(client, data);
  517. mutex_init(&data->update_lock);
  518. data->type = id->driver_data;
  519. data->cmdreg = i2c_new_dummy(adapter, (client->addr & ~SMM665_REGMASK)
  520. | SMM665_CMDREG_BASE);
  521. if (!data->cmdreg)
  522. goto out_kfree;
  523. switch (data->type) {
  524. case smm465:
  525. case smm665:
  526. data->conversion_time = SMM665_ADC_WAIT_SMM665;
  527. break;
  528. case smm665c:
  529. case smm764:
  530. case smm766:
  531. data->conversion_time = SMM665_ADC_WAIT_SMM766;
  532. break;
  533. }
  534. ret = -ENODEV;
  535. if (i2c_smbus_read_byte_data(data->cmdreg, SMM665_MISC8_CMD_STS) < 0)
  536. goto out_unregister;
  537. /*
  538. * Read limits.
  539. *
  540. * Limit registers start with register SMM665_LIMIT_BASE.
  541. * Each channel uses 8 registers, providing four limit values
  542. * per channel. Each limit value requires two registers, with the
  543. * high byte in the first register and the low byte in the second
  544. * register. The first two limits are under limit values, followed
  545. * by two over limit values.
  546. *
  547. * Limit register order matches the ADC register order, so we use
  548. * ADC register defines throughout the code to index limit registers.
  549. *
  550. * We save the first retrieved value both as "critical" and "alarm"
  551. * value. The second value overwrites either the critical or the
  552. * alarm value, depending on its configuration. This ensures that both
  553. * critical and alarm values are initialized, even if both registers are
  554. * configured as critical or non-critical.
  555. */
  556. for (i = 0; i < SMM665_NUM_ADC; i++) {
  557. int val;
  558. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8);
  559. if (unlikely(val < 0))
  560. goto out_unregister;
  561. data->critical_min_limit[i] = data->alarm_min_limit[i]
  562. = smm665_convert(val, i);
  563. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 2);
  564. if (unlikely(val < 0))
  565. goto out_unregister;
  566. if (smm665_is_critical(val))
  567. data->critical_min_limit[i] = smm665_convert(val, i);
  568. else
  569. data->alarm_min_limit[i] = smm665_convert(val, i);
  570. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 4);
  571. if (unlikely(val < 0))
  572. goto out_unregister;
  573. data->critical_max_limit[i] = data->alarm_max_limit[i]
  574. = smm665_convert(val, i);
  575. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 6);
  576. if (unlikely(val < 0))
  577. goto out_unregister;
  578. if (smm665_is_critical(val))
  579. data->critical_max_limit[i] = smm665_convert(val, i);
  580. else
  581. data->alarm_max_limit[i] = smm665_convert(val, i);
  582. }
  583. /* Register sysfs hooks */
  584. ret = sysfs_create_group(&client->dev.kobj, &smm665_group);
  585. if (ret)
  586. goto out_unregister;
  587. data->hwmon_dev = hwmon_device_register(&client->dev);
  588. if (IS_ERR(data->hwmon_dev)) {
  589. ret = PTR_ERR(data->hwmon_dev);
  590. goto out_remove_group;
  591. }
  592. return 0;
  593. out_remove_group:
  594. sysfs_remove_group(&client->dev.kobj, &smm665_group);
  595. out_unregister:
  596. i2c_unregister_device(data->cmdreg);
  597. out_kfree:
  598. kfree(data);
  599. out_return:
  600. return ret;
  601. }
  602. static int smm665_remove(struct i2c_client *client)
  603. {
  604. struct smm665_data *data = i2c_get_clientdata(client);
  605. i2c_unregister_device(data->cmdreg);
  606. hwmon_device_unregister(data->hwmon_dev);
  607. sysfs_remove_group(&client->dev.kobj, &smm665_group);
  608. kfree(data);
  609. return 0;
  610. }
  611. static const struct i2c_device_id smm665_id[] = {
  612. {"smm465", smm465},
  613. {"smm665", smm665},
  614. {"smm665c", smm665c},
  615. {"smm764", smm764},
  616. {"smm766", smm766},
  617. {}
  618. };
  619. MODULE_DEVICE_TABLE(i2c, smm665_id);
  620. /* This is the driver that will be inserted */
  621. static struct i2c_driver smm665_driver = {
  622. .driver = {
  623. .name = "smm665",
  624. },
  625. .probe = smm665_probe,
  626. .remove = smm665_remove,
  627. .id_table = smm665_id,
  628. };
  629. static int __init smm665_init(void)
  630. {
  631. return i2c_add_driver(&smm665_driver);
  632. }
  633. static void __exit smm665_exit(void)
  634. {
  635. i2c_del_driver(&smm665_driver);
  636. }
  637. MODULE_AUTHOR("Guenter Roeck");
  638. MODULE_DESCRIPTION("SMM665 driver");
  639. MODULE_LICENSE("GPL");
  640. module_init(smm665_init);
  641. module_exit(smm665_exit);