max16065.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * Driver for
  3. * Maxim MAX16065/MAX16066 12-Channel/8-Channel, Flash-Configurable
  4. * System Managers with Nonvolatile Fault Registers
  5. * Maxim MAX16067/MAX16068 6-Channel, Flash-Configurable System Managers
  6. * with Nonvolatile Fault Registers
  7. * Maxim MAX16070/MAX16071 12-Channel/8-Channel, Flash-Configurable System
  8. * Monitors with Nonvolatile Fault Registers
  9. *
  10. * Copyright (C) 2011 Ericsson AB.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; version 2 of the License.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/i2c.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/hwmon-sysfs.h>
  24. #include <linux/delay.h>
  25. #include <linux/jiffies.h>
  26. enum chips { max16065, max16066, max16067, max16068, max16070, max16071 };
  27. /*
  28. * Registers
  29. */
  30. #define MAX16065_ADC(x) ((x) * 2)
  31. #define MAX16065_CURR_SENSE 0x18
  32. #define MAX16065_CSP_ADC 0x19
  33. #define MAX16065_FAULT(x) (0x1b + (x))
  34. #define MAX16065_SCALE(x) (0x43 + (x))
  35. #define MAX16065_CURR_CONTROL 0x47
  36. #define MAX16065_LIMIT(l, x) (0x48 + (l) + (x) * 3) /*
  37. * l: limit
  38. * 0: min/max
  39. * 1: crit
  40. * 2: lcrit
  41. * x: ADC index
  42. */
  43. #define MAX16065_SW_ENABLE 0x73
  44. #define MAX16065_WARNING_OV (1 << 3) /* Set if secondary threshold is OV
  45. warning */
  46. #define MAX16065_CURR_ENABLE (1 << 0)
  47. #define MAX16065_NUM_LIMIT 3
  48. #define MAX16065_NUM_ADC 12 /* maximum number of ADC channels */
  49. static const int max16065_num_adc[] = {
  50. [max16065] = 12,
  51. [max16066] = 8,
  52. [max16067] = 6,
  53. [max16068] = 6,
  54. [max16070] = 12,
  55. [max16071] = 8,
  56. };
  57. static const bool max16065_have_secondary[] = {
  58. [max16065] = true,
  59. [max16066] = true,
  60. [max16067] = false,
  61. [max16068] = false,
  62. [max16070] = true,
  63. [max16071] = true,
  64. };
  65. static const bool max16065_have_current[] = {
  66. [max16065] = true,
  67. [max16066] = true,
  68. [max16067] = false,
  69. [max16068] = false,
  70. [max16070] = true,
  71. [max16071] = true,
  72. };
  73. struct max16065_data {
  74. enum chips type;
  75. struct device *hwmon_dev;
  76. struct mutex update_lock;
  77. bool valid;
  78. unsigned long last_updated; /* in jiffies */
  79. int num_adc;
  80. bool have_current;
  81. int curr_gain;
  82. /* limits are in mV */
  83. int limit[MAX16065_NUM_LIMIT][MAX16065_NUM_ADC];
  84. int range[MAX16065_NUM_ADC + 1];/* voltage range */
  85. int adc[MAX16065_NUM_ADC + 1]; /* adc values (raw) including csp_adc */
  86. int curr_sense;
  87. int fault[2];
  88. };
  89. static const int max16065_adc_range[] = { 5560, 2780, 1390, 0 };
  90. static const int max16065_csp_adc_range[] = { 7000, 14000 };
  91. /* ADC registers have 10 bit resolution. */
  92. static inline int ADC_TO_MV(int adc, int range)
  93. {
  94. return (adc * range) / 1024;
  95. }
  96. /*
  97. * Limit registers have 8 bit resolution and match upper 8 bits of ADC
  98. * registers.
  99. */
  100. static inline int LIMIT_TO_MV(int limit, int range)
  101. {
  102. return limit * range / 256;
  103. }
  104. static inline int MV_TO_LIMIT(int mv, int range)
  105. {
  106. return SENSORS_LIMIT(DIV_ROUND_CLOSEST(mv * 256, range), 0, 255);
  107. }
  108. static inline int ADC_TO_CURR(int adc, int gain)
  109. {
  110. return adc * 1400000 / (gain * 255);
  111. }
  112. /*
  113. * max16065_read_adc()
  114. *
  115. * Read 16 bit value from <reg>, <reg+1>.
  116. * Upper 8 bits are in <reg>, lower 2 bits are in bits 7:6 of <reg+1>.
  117. */
  118. static int max16065_read_adc(struct i2c_client *client, int reg)
  119. {
  120. int rv;
  121. rv = i2c_smbus_read_word_data(client, reg);
  122. if (unlikely(rv < 0))
  123. return rv;
  124. return ((rv & 0xff) << 2) | ((rv >> 14) & 0x03);
  125. }
  126. static struct max16065_data *max16065_update_device(struct device *dev)
  127. {
  128. struct i2c_client *client = to_i2c_client(dev);
  129. struct max16065_data *data = i2c_get_clientdata(client);
  130. mutex_lock(&data->update_lock);
  131. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  132. int i;
  133. for (i = 0; i < data->num_adc; i++)
  134. data->adc[i]
  135. = max16065_read_adc(client, MAX16065_ADC(i));
  136. if (data->have_current) {
  137. data->adc[MAX16065_NUM_ADC]
  138. = max16065_read_adc(client, MAX16065_CSP_ADC);
  139. data->curr_sense
  140. = i2c_smbus_read_byte_data(client,
  141. MAX16065_CURR_SENSE);
  142. }
  143. for (i = 0; i < DIV_ROUND_UP(data->num_adc, 8); i++)
  144. data->fault[i]
  145. = i2c_smbus_read_byte_data(client, MAX16065_FAULT(i));
  146. data->last_updated = jiffies;
  147. data->valid = 1;
  148. }
  149. mutex_unlock(&data->update_lock);
  150. return data;
  151. }
  152. static ssize_t max16065_show_alarm(struct device *dev,
  153. struct device_attribute *da, char *buf)
  154. {
  155. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
  156. struct max16065_data *data = max16065_update_device(dev);
  157. int val = data->fault[attr2->nr];
  158. if (val < 0)
  159. return val;
  160. val &= (1 << attr2->index);
  161. if (val)
  162. i2c_smbus_write_byte_data(to_i2c_client(dev),
  163. MAX16065_FAULT(attr2->nr), val);
  164. return snprintf(buf, PAGE_SIZE, "%d\n", !!val);
  165. }
  166. static ssize_t max16065_show_input(struct device *dev,
  167. struct device_attribute *da, char *buf)
  168. {
  169. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  170. struct max16065_data *data = max16065_update_device(dev);
  171. int adc = data->adc[attr->index];
  172. if (unlikely(adc < 0))
  173. return adc;
  174. return snprintf(buf, PAGE_SIZE, "%d\n",
  175. ADC_TO_MV(adc, data->range[attr->index]));
  176. }
  177. static ssize_t max16065_show_current(struct device *dev,
  178. struct device_attribute *da, char *buf)
  179. {
  180. struct max16065_data *data = max16065_update_device(dev);
  181. if (unlikely(data->curr_sense < 0))
  182. return data->curr_sense;
  183. return snprintf(buf, PAGE_SIZE, "%d\n",
  184. ADC_TO_CURR(data->curr_sense, data->curr_gain));
  185. }
  186. static ssize_t max16065_set_limit(struct device *dev,
  187. struct device_attribute *da,
  188. const char *buf, size_t count)
  189. {
  190. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
  191. struct i2c_client *client = to_i2c_client(dev);
  192. struct max16065_data *data = i2c_get_clientdata(client);
  193. unsigned long val;
  194. int err;
  195. int limit;
  196. err = strict_strtoul(buf, 10, &val);
  197. if (unlikely(err < 0))
  198. return err;
  199. limit = MV_TO_LIMIT(val, data->range[attr2->index]);
  200. mutex_lock(&data->update_lock);
  201. data->limit[attr2->nr][attr2->index]
  202. = LIMIT_TO_MV(limit, data->range[attr2->index]);
  203. i2c_smbus_write_byte_data(client,
  204. MAX16065_LIMIT(attr2->nr, attr2->index),
  205. limit);
  206. mutex_unlock(&data->update_lock);
  207. return count;
  208. }
  209. static ssize_t max16065_show_limit(struct device *dev,
  210. struct device_attribute *da, char *buf)
  211. {
  212. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
  213. struct i2c_client *client = to_i2c_client(dev);
  214. struct max16065_data *data = i2c_get_clientdata(client);
  215. return snprintf(buf, PAGE_SIZE, "%d\n",
  216. data->limit[attr2->nr][attr2->index]);
  217. }
  218. /* Construct a sensor_device_attribute structure for each register */
  219. /* Input voltages */
  220. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, max16065_show_input, NULL, 0);
  221. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, max16065_show_input, NULL, 1);
  222. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, max16065_show_input, NULL, 2);
  223. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, max16065_show_input, NULL, 3);
  224. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, max16065_show_input, NULL, 4);
  225. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, max16065_show_input, NULL, 5);
  226. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, max16065_show_input, NULL, 6);
  227. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, max16065_show_input, NULL, 7);
  228. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, max16065_show_input, NULL, 8);
  229. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, max16065_show_input, NULL, 9);
  230. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, max16065_show_input, NULL, 10);
  231. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, max16065_show_input, NULL, 11);
  232. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, max16065_show_input, NULL, 12);
  233. /* Input voltages lcrit */
  234. static SENSOR_DEVICE_ATTR_2(in0_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  235. max16065_set_limit, 2, 0);
  236. static SENSOR_DEVICE_ATTR_2(in1_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  237. max16065_set_limit, 2, 1);
  238. static SENSOR_DEVICE_ATTR_2(in2_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  239. max16065_set_limit, 2, 2);
  240. static SENSOR_DEVICE_ATTR_2(in3_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  241. max16065_set_limit, 2, 3);
  242. static SENSOR_DEVICE_ATTR_2(in4_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  243. max16065_set_limit, 2, 4);
  244. static SENSOR_DEVICE_ATTR_2(in5_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  245. max16065_set_limit, 2, 5);
  246. static SENSOR_DEVICE_ATTR_2(in6_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  247. max16065_set_limit, 2, 6);
  248. static SENSOR_DEVICE_ATTR_2(in7_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  249. max16065_set_limit, 2, 7);
  250. static SENSOR_DEVICE_ATTR_2(in8_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  251. max16065_set_limit, 2, 8);
  252. static SENSOR_DEVICE_ATTR_2(in9_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  253. max16065_set_limit, 2, 9);
  254. static SENSOR_DEVICE_ATTR_2(in10_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  255. max16065_set_limit, 2, 10);
  256. static SENSOR_DEVICE_ATTR_2(in11_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  257. max16065_set_limit, 2, 11);
  258. /* Input voltages crit */
  259. static SENSOR_DEVICE_ATTR_2(in0_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  260. max16065_set_limit, 1, 0);
  261. static SENSOR_DEVICE_ATTR_2(in1_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  262. max16065_set_limit, 1, 1);
  263. static SENSOR_DEVICE_ATTR_2(in2_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  264. max16065_set_limit, 1, 2);
  265. static SENSOR_DEVICE_ATTR_2(in3_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  266. max16065_set_limit, 1, 3);
  267. static SENSOR_DEVICE_ATTR_2(in4_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  268. max16065_set_limit, 1, 4);
  269. static SENSOR_DEVICE_ATTR_2(in5_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  270. max16065_set_limit, 1, 5);
  271. static SENSOR_DEVICE_ATTR_2(in6_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  272. max16065_set_limit, 1, 6);
  273. static SENSOR_DEVICE_ATTR_2(in7_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  274. max16065_set_limit, 1, 7);
  275. static SENSOR_DEVICE_ATTR_2(in8_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  276. max16065_set_limit, 1, 8);
  277. static SENSOR_DEVICE_ATTR_2(in9_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  278. max16065_set_limit, 1, 9);
  279. static SENSOR_DEVICE_ATTR_2(in10_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  280. max16065_set_limit, 1, 10);
  281. static SENSOR_DEVICE_ATTR_2(in11_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  282. max16065_set_limit, 1, 11);
  283. /* Input voltages min */
  284. static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  285. max16065_set_limit, 0, 0);
  286. static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  287. max16065_set_limit, 0, 1);
  288. static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  289. max16065_set_limit, 0, 2);
  290. static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  291. max16065_set_limit, 0, 3);
  292. static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  293. max16065_set_limit, 0, 4);
  294. static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  295. max16065_set_limit, 0, 5);
  296. static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  297. max16065_set_limit, 0, 6);
  298. static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  299. max16065_set_limit, 0, 7);
  300. static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  301. max16065_set_limit, 0, 8);
  302. static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  303. max16065_set_limit, 0, 9);
  304. static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  305. max16065_set_limit, 0, 10);
  306. static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  307. max16065_set_limit, 0, 11);
  308. /* Input voltages max */
  309. static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  310. max16065_set_limit, 0, 0);
  311. static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  312. max16065_set_limit, 0, 1);
  313. static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  314. max16065_set_limit, 0, 2);
  315. static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  316. max16065_set_limit, 0, 3);
  317. static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  318. max16065_set_limit, 0, 4);
  319. static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  320. max16065_set_limit, 0, 5);
  321. static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  322. max16065_set_limit, 0, 6);
  323. static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  324. max16065_set_limit, 0, 7);
  325. static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  326. max16065_set_limit, 0, 8);
  327. static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  328. max16065_set_limit, 0, 9);
  329. static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  330. max16065_set_limit, 0, 10);
  331. static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  332. max16065_set_limit, 0, 11);
  333. /* alarms */
  334. static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, max16065_show_alarm, NULL,
  335. 0, 0);
  336. static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, max16065_show_alarm, NULL,
  337. 0, 1);
  338. static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, max16065_show_alarm, NULL,
  339. 0, 2);
  340. static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, max16065_show_alarm, NULL,
  341. 0, 3);
  342. static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, max16065_show_alarm, NULL,
  343. 0, 4);
  344. static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, max16065_show_alarm, NULL,
  345. 0, 5);
  346. static SENSOR_DEVICE_ATTR_2(in6_alarm, S_IRUGO, max16065_show_alarm, NULL,
  347. 0, 6);
  348. static SENSOR_DEVICE_ATTR_2(in7_alarm, S_IRUGO, max16065_show_alarm, NULL,
  349. 0, 7);
  350. static SENSOR_DEVICE_ATTR_2(in8_alarm, S_IRUGO, max16065_show_alarm, NULL,
  351. 1, 0);
  352. static SENSOR_DEVICE_ATTR_2(in9_alarm, S_IRUGO, max16065_show_alarm, NULL,
  353. 1, 1);
  354. static SENSOR_DEVICE_ATTR_2(in10_alarm, S_IRUGO, max16065_show_alarm, NULL,
  355. 1, 2);
  356. static SENSOR_DEVICE_ATTR_2(in11_alarm, S_IRUGO, max16065_show_alarm, NULL,
  357. 1, 3);
  358. /* Current and alarm */
  359. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, max16065_show_current, NULL, 0);
  360. static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, max16065_show_alarm, NULL,
  361. 1, 4);
  362. /*
  363. * Finally, construct an array of pointers to members of the above objects,
  364. * as required for sysfs_create_group()
  365. */
  366. static struct attribute *max16065_basic_attributes[] = {
  367. &sensor_dev_attr_in0_input.dev_attr.attr,
  368. &sensor_dev_attr_in0_lcrit.dev_attr.attr,
  369. &sensor_dev_attr_in0_crit.dev_attr.attr,
  370. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  371. &sensor_dev_attr_in1_input.dev_attr.attr,
  372. &sensor_dev_attr_in1_lcrit.dev_attr.attr,
  373. &sensor_dev_attr_in1_crit.dev_attr.attr,
  374. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  375. &sensor_dev_attr_in2_input.dev_attr.attr,
  376. &sensor_dev_attr_in2_lcrit.dev_attr.attr,
  377. &sensor_dev_attr_in2_crit.dev_attr.attr,
  378. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  379. &sensor_dev_attr_in3_input.dev_attr.attr,
  380. &sensor_dev_attr_in3_lcrit.dev_attr.attr,
  381. &sensor_dev_attr_in3_crit.dev_attr.attr,
  382. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  383. &sensor_dev_attr_in4_input.dev_attr.attr,
  384. &sensor_dev_attr_in4_lcrit.dev_attr.attr,
  385. &sensor_dev_attr_in4_crit.dev_attr.attr,
  386. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  387. &sensor_dev_attr_in5_input.dev_attr.attr,
  388. &sensor_dev_attr_in5_lcrit.dev_attr.attr,
  389. &sensor_dev_attr_in5_crit.dev_attr.attr,
  390. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  391. &sensor_dev_attr_in6_input.dev_attr.attr,
  392. &sensor_dev_attr_in6_lcrit.dev_attr.attr,
  393. &sensor_dev_attr_in6_crit.dev_attr.attr,
  394. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  395. &sensor_dev_attr_in7_input.dev_attr.attr,
  396. &sensor_dev_attr_in7_lcrit.dev_attr.attr,
  397. &sensor_dev_attr_in7_crit.dev_attr.attr,
  398. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  399. &sensor_dev_attr_in8_input.dev_attr.attr,
  400. &sensor_dev_attr_in8_lcrit.dev_attr.attr,
  401. &sensor_dev_attr_in8_crit.dev_attr.attr,
  402. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  403. &sensor_dev_attr_in9_input.dev_attr.attr,
  404. &sensor_dev_attr_in9_lcrit.dev_attr.attr,
  405. &sensor_dev_attr_in9_crit.dev_attr.attr,
  406. &sensor_dev_attr_in9_alarm.dev_attr.attr,
  407. &sensor_dev_attr_in10_input.dev_attr.attr,
  408. &sensor_dev_attr_in10_lcrit.dev_attr.attr,
  409. &sensor_dev_attr_in10_crit.dev_attr.attr,
  410. &sensor_dev_attr_in10_alarm.dev_attr.attr,
  411. &sensor_dev_attr_in11_input.dev_attr.attr,
  412. &sensor_dev_attr_in11_lcrit.dev_attr.attr,
  413. &sensor_dev_attr_in11_crit.dev_attr.attr,
  414. &sensor_dev_attr_in11_alarm.dev_attr.attr,
  415. NULL
  416. };
  417. static struct attribute *max16065_current_attributes[] = {
  418. &sensor_dev_attr_in12_input.dev_attr.attr,
  419. &sensor_dev_attr_curr1_input.dev_attr.attr,
  420. &sensor_dev_attr_curr1_alarm.dev_attr.attr,
  421. NULL
  422. };
  423. static struct attribute *max16065_min_attributes[] = {
  424. &sensor_dev_attr_in0_min.dev_attr.attr,
  425. &sensor_dev_attr_in1_min.dev_attr.attr,
  426. &sensor_dev_attr_in2_min.dev_attr.attr,
  427. &sensor_dev_attr_in3_min.dev_attr.attr,
  428. &sensor_dev_attr_in4_min.dev_attr.attr,
  429. &sensor_dev_attr_in5_min.dev_attr.attr,
  430. &sensor_dev_attr_in6_min.dev_attr.attr,
  431. &sensor_dev_attr_in7_min.dev_attr.attr,
  432. &sensor_dev_attr_in8_min.dev_attr.attr,
  433. &sensor_dev_attr_in9_min.dev_attr.attr,
  434. &sensor_dev_attr_in10_min.dev_attr.attr,
  435. &sensor_dev_attr_in11_min.dev_attr.attr,
  436. NULL
  437. };
  438. static struct attribute *max16065_max_attributes[] = {
  439. &sensor_dev_attr_in0_max.dev_attr.attr,
  440. &sensor_dev_attr_in1_max.dev_attr.attr,
  441. &sensor_dev_attr_in2_max.dev_attr.attr,
  442. &sensor_dev_attr_in3_max.dev_attr.attr,
  443. &sensor_dev_attr_in4_max.dev_attr.attr,
  444. &sensor_dev_attr_in5_max.dev_attr.attr,
  445. &sensor_dev_attr_in6_max.dev_attr.attr,
  446. &sensor_dev_attr_in7_max.dev_attr.attr,
  447. &sensor_dev_attr_in8_max.dev_attr.attr,
  448. &sensor_dev_attr_in9_max.dev_attr.attr,
  449. &sensor_dev_attr_in10_max.dev_attr.attr,
  450. &sensor_dev_attr_in11_max.dev_attr.attr,
  451. NULL
  452. };
  453. static const struct attribute_group max16065_basic_group = {
  454. .attrs = max16065_basic_attributes,
  455. };
  456. static const struct attribute_group max16065_current_group = {
  457. .attrs = max16065_current_attributes,
  458. };
  459. static const struct attribute_group max16065_min_group = {
  460. .attrs = max16065_min_attributes,
  461. };
  462. static const struct attribute_group max16065_max_group = {
  463. .attrs = max16065_max_attributes,
  464. };
  465. static void max16065_cleanup(struct i2c_client *client)
  466. {
  467. sysfs_remove_group(&client->dev.kobj, &max16065_max_group);
  468. sysfs_remove_group(&client->dev.kobj, &max16065_min_group);
  469. sysfs_remove_group(&client->dev.kobj, &max16065_current_group);
  470. sysfs_remove_group(&client->dev.kobj, &max16065_basic_group);
  471. }
  472. static int max16065_probe(struct i2c_client *client,
  473. const struct i2c_device_id *id)
  474. {
  475. struct i2c_adapter *adapter = client->adapter;
  476. struct max16065_data *data;
  477. int i, j, val, ret;
  478. bool have_secondary; /* true if chip has secondary limits */
  479. bool secondary_is_max = false; /* secondary limits reflect max */
  480. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  481. | I2C_FUNC_SMBUS_READ_WORD_DATA))
  482. return -ENODEV;
  483. data = kzalloc(sizeof(*data), GFP_KERNEL);
  484. if (unlikely(!data))
  485. return -ENOMEM;
  486. i2c_set_clientdata(client, data);
  487. mutex_init(&data->update_lock);
  488. data->num_adc = max16065_num_adc[id->driver_data];
  489. data->have_current = max16065_have_current[id->driver_data];
  490. have_secondary = max16065_have_secondary[id->driver_data];
  491. if (have_secondary) {
  492. val = i2c_smbus_read_byte_data(client, MAX16065_SW_ENABLE);
  493. if (unlikely(val < 0)) {
  494. ret = val;
  495. goto out_free;
  496. }
  497. secondary_is_max = val & MAX16065_WARNING_OV;
  498. }
  499. /* Read scale registers, convert to range */
  500. for (i = 0; i < DIV_ROUND_UP(data->num_adc, 4); i++) {
  501. val = i2c_smbus_read_byte_data(client, MAX16065_SCALE(i));
  502. if (unlikely(val < 0)) {
  503. ret = val;
  504. goto out_free;
  505. }
  506. for (j = 0; j < 4 && i * 4 + j < data->num_adc; j++) {
  507. data->range[i * 4 + j] =
  508. max16065_adc_range[(val >> (j * 2)) & 0x3];
  509. }
  510. }
  511. /* Read limits */
  512. for (i = 0; i < MAX16065_NUM_LIMIT; i++) {
  513. if (i == 0 && !have_secondary)
  514. continue;
  515. for (j = 0; j < data->num_adc; j++) {
  516. val = i2c_smbus_read_byte_data(client,
  517. MAX16065_LIMIT(i, j));
  518. if (unlikely(val < 0)) {
  519. ret = val;
  520. goto out_free;
  521. }
  522. data->limit[i][j] = LIMIT_TO_MV(val, data->range[j]);
  523. }
  524. }
  525. /* Register sysfs hooks */
  526. for (i = 0; i < data->num_adc * 4; i++) {
  527. /* Do not create sysfs entry if channel is disabled */
  528. if (!data->range[i / 4])
  529. continue;
  530. ret = sysfs_create_file(&client->dev.kobj,
  531. max16065_basic_attributes[i]);
  532. if (unlikely(ret))
  533. goto out;
  534. }
  535. if (have_secondary) {
  536. struct attribute **attr = secondary_is_max ?
  537. max16065_max_attributes : max16065_min_attributes;
  538. for (i = 0; i < data->num_adc; i++) {
  539. if (!data->range[i])
  540. continue;
  541. ret = sysfs_create_file(&client->dev.kobj, attr[i]);
  542. if (unlikely(ret))
  543. goto out;
  544. }
  545. }
  546. if (data->have_current) {
  547. val = i2c_smbus_read_byte_data(client, MAX16065_CURR_CONTROL);
  548. if (unlikely(val < 0)) {
  549. ret = val;
  550. goto out;
  551. }
  552. if (val & MAX16065_CURR_ENABLE) {
  553. /*
  554. * Current gain is 6, 12, 24, 48 based on values in
  555. * bit 2,3.
  556. */
  557. data->curr_gain = 6 << ((val >> 2) & 0x03);
  558. data->range[MAX16065_NUM_ADC]
  559. = max16065_csp_adc_range[(val >> 1) & 0x01];
  560. ret = sysfs_create_group(&client->dev.kobj,
  561. &max16065_current_group);
  562. if (unlikely(ret))
  563. goto out;
  564. } else {
  565. data->have_current = false;
  566. }
  567. }
  568. data->hwmon_dev = hwmon_device_register(&client->dev);
  569. if (unlikely(IS_ERR(data->hwmon_dev))) {
  570. ret = PTR_ERR(data->hwmon_dev);
  571. goto out;
  572. }
  573. return 0;
  574. out:
  575. max16065_cleanup(client);
  576. out_free:
  577. kfree(data);
  578. return ret;
  579. }
  580. static int max16065_remove(struct i2c_client *client)
  581. {
  582. struct max16065_data *data = i2c_get_clientdata(client);
  583. hwmon_device_unregister(data->hwmon_dev);
  584. max16065_cleanup(client);
  585. kfree(data);
  586. return 0;
  587. }
  588. static const struct i2c_device_id max16065_id[] = {
  589. { "max16065", max16065 },
  590. { "max16066", max16066 },
  591. { "max16067", max16067 },
  592. { "max16068", max16068 },
  593. { "max16070", max16070 },
  594. { "max16071", max16071 },
  595. { }
  596. };
  597. MODULE_DEVICE_TABLE(i2c, max16065_id);
  598. /* This is the driver that will be inserted */
  599. static struct i2c_driver max16065_driver = {
  600. .driver = {
  601. .name = "max16065",
  602. },
  603. .probe = max16065_probe,
  604. .remove = max16065_remove,
  605. .id_table = max16065_id,
  606. };
  607. static int __init max16065_init(void)
  608. {
  609. return i2c_add_driver(&max16065_driver);
  610. }
  611. static void __exit max16065_exit(void)
  612. {
  613. i2c_del_driver(&max16065_driver);
  614. }
  615. MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
  616. MODULE_DESCRIPTION("MAX16065 driver");
  617. MODULE_LICENSE("GPL");
  618. module_init(max16065_init);
  619. module_exit(max16065_exit);