pmbus_core.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /*
  2. * Hardware monitoring driver for PMBus devices
  3. *
  4. * Copyright (c) 2010, 2011 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; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/delay.h>
  29. #include <linux/i2c/pmbus.h>
  30. #include "pmbus.h"
  31. /*
  32. * Constants needed to determine number of sensors, booleans, and labels.
  33. */
  34. #define PMBUS_MAX_INPUT_SENSORS 11 /* 6*volt, 3*curr, 2*power */
  35. #define PMBUS_VOUT_SENSORS_PER_PAGE 5 /* input, min, max, lcrit,
  36. crit */
  37. #define PMBUS_IOUT_SENSORS_PER_PAGE 4 /* input, min, max, crit */
  38. #define PMBUS_POUT_SENSORS_PER_PAGE 4 /* input, cap, max, crit */
  39. #define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */
  40. #define PMBUS_MAX_SENSORS_PER_TEMP 5 /* input, min, max, lcrit,
  41. crit */
  42. #define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm,
  43. lcrit_alarm, crit_alarm;
  44. c: alarm, crit_alarm;
  45. p: crit_alarm */
  46. #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm,
  47. lcrit_alarm, crit_alarm */
  48. #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm,
  49. crit_alarm */
  50. #define PMBUS_POUT_BOOLEANS_PER_PAGE 3 /* cap_alarm, alarm, crit_alarm
  51. */
  52. #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
  53. #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
  54. lcrit_alarm, crit_alarm */
  55. #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
  56. /*
  57. * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
  58. * are paged. status_input is unpaged.
  59. */
  60. #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
  61. /*
  62. * Index into status register array, per status register group
  63. */
  64. #define PB_STATUS_BASE 0
  65. #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
  66. #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
  67. #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
  68. #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
  69. #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
  70. #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
  71. struct pmbus_sensor {
  72. char name[I2C_NAME_SIZE]; /* sysfs sensor name */
  73. struct sensor_device_attribute attribute;
  74. u8 page; /* page number */
  75. u8 reg; /* register */
  76. enum pmbus_sensor_classes class; /* sensor class */
  77. bool update; /* runtime sensor update needed */
  78. int data; /* Sensor data.
  79. Negative if there was a read error */
  80. };
  81. struct pmbus_boolean {
  82. char name[I2C_NAME_SIZE]; /* sysfs boolean name */
  83. struct sensor_device_attribute attribute;
  84. };
  85. struct pmbus_label {
  86. char name[I2C_NAME_SIZE]; /* sysfs label name */
  87. struct sensor_device_attribute attribute;
  88. char label[I2C_NAME_SIZE]; /* label */
  89. };
  90. struct pmbus_data {
  91. struct device *hwmon_dev;
  92. u32 flags; /* from platform data */
  93. int exponent; /* linear mode: exponent for output voltages */
  94. const struct pmbus_driver_info *info;
  95. int max_attributes;
  96. int num_attributes;
  97. struct attribute **attributes;
  98. struct attribute_group group;
  99. /*
  100. * Sensors cover both sensor and limit registers.
  101. */
  102. int max_sensors;
  103. int num_sensors;
  104. struct pmbus_sensor *sensors;
  105. /*
  106. * Booleans are used for alarms.
  107. * Values are determined from status registers.
  108. */
  109. int max_booleans;
  110. int num_booleans;
  111. struct pmbus_boolean *booleans;
  112. /*
  113. * Labels are used to map generic names (e.g., "in1")
  114. * to PMBus specific names (e.g., "vin" or "vout1").
  115. */
  116. int max_labels;
  117. int num_labels;
  118. struct pmbus_label *labels;
  119. struct mutex update_lock;
  120. bool valid;
  121. unsigned long last_updated; /* in jiffies */
  122. /*
  123. * A single status register covers multiple attributes,
  124. * so we keep them all together.
  125. */
  126. u8 status[PB_NUM_STATUS_REG];
  127. u8 currpage;
  128. };
  129. int pmbus_set_page(struct i2c_client *client, u8 page)
  130. {
  131. struct pmbus_data *data = i2c_get_clientdata(client);
  132. int rv = 0;
  133. int newpage;
  134. if (page != data->currpage) {
  135. rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
  136. newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
  137. if (newpage != page)
  138. rv = -EINVAL;
  139. else
  140. data->currpage = page;
  141. }
  142. return rv;
  143. }
  144. EXPORT_SYMBOL_GPL(pmbus_set_page);
  145. static int pmbus_write_byte(struct i2c_client *client, u8 page, u8 value)
  146. {
  147. int rv;
  148. rv = pmbus_set_page(client, page);
  149. if (rv < 0)
  150. return rv;
  151. return i2c_smbus_write_byte(client, value);
  152. }
  153. static int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg,
  154. u16 word)
  155. {
  156. int rv;
  157. rv = pmbus_set_page(client, page);
  158. if (rv < 0)
  159. return rv;
  160. return i2c_smbus_write_word_data(client, reg, word);
  161. }
  162. int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg)
  163. {
  164. int rv;
  165. rv = pmbus_set_page(client, page);
  166. if (rv < 0)
  167. return rv;
  168. return i2c_smbus_read_word_data(client, reg);
  169. }
  170. EXPORT_SYMBOL_GPL(pmbus_read_word_data);
  171. static int pmbus_read_byte_data(struct i2c_client *client, u8 page, u8 reg)
  172. {
  173. int rv;
  174. rv = pmbus_set_page(client, page);
  175. if (rv < 0)
  176. return rv;
  177. return i2c_smbus_read_byte_data(client, reg);
  178. }
  179. static void pmbus_clear_fault_page(struct i2c_client *client, int page)
  180. {
  181. pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
  182. }
  183. void pmbus_clear_faults(struct i2c_client *client)
  184. {
  185. struct pmbus_data *data = i2c_get_clientdata(client);
  186. int i;
  187. for (i = 0; i < data->info->pages; i++)
  188. pmbus_clear_fault_page(client, i);
  189. }
  190. EXPORT_SYMBOL_GPL(pmbus_clear_faults);
  191. static int pmbus_check_status_cml(struct i2c_client *client, int page)
  192. {
  193. int status, status2;
  194. status = pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
  195. if (status < 0 || (status & PB_STATUS_CML)) {
  196. status2 = pmbus_read_byte_data(client, page, PMBUS_STATUS_CML);
  197. if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
  198. return -EINVAL;
  199. }
  200. return 0;
  201. }
  202. bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
  203. {
  204. int rv;
  205. struct pmbus_data *data = i2c_get_clientdata(client);
  206. rv = pmbus_read_byte_data(client, page, reg);
  207. if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
  208. rv = pmbus_check_status_cml(client, page);
  209. pmbus_clear_fault_page(client, page);
  210. return rv >= 0;
  211. }
  212. EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
  213. bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
  214. {
  215. int rv;
  216. struct pmbus_data *data = i2c_get_clientdata(client);
  217. rv = pmbus_read_word_data(client, page, reg);
  218. if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
  219. rv = pmbus_check_status_cml(client, page);
  220. pmbus_clear_fault_page(client, page);
  221. return rv >= 0;
  222. }
  223. EXPORT_SYMBOL_GPL(pmbus_check_word_register);
  224. const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
  225. {
  226. struct pmbus_data *data = i2c_get_clientdata(client);
  227. return data->info;
  228. }
  229. EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
  230. /*
  231. * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
  232. * a device specific mapping funcion exists and calls it if necessary.
  233. */
  234. static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
  235. {
  236. struct pmbus_data *data = i2c_get_clientdata(client);
  237. const struct pmbus_driver_info *info = data->info;
  238. int status;
  239. if (info->read_byte_data) {
  240. status = info->read_byte_data(client, page, reg);
  241. if (status != -ENODATA)
  242. return status;
  243. }
  244. return pmbus_read_byte_data(client, page, reg);
  245. }
  246. static struct pmbus_data *pmbus_update_device(struct device *dev)
  247. {
  248. struct i2c_client *client = to_i2c_client(dev);
  249. struct pmbus_data *data = i2c_get_clientdata(client);
  250. const struct pmbus_driver_info *info = data->info;
  251. mutex_lock(&data->update_lock);
  252. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  253. int i;
  254. for (i = 0; i < info->pages; i++)
  255. data->status[PB_STATUS_BASE + i]
  256. = pmbus_read_byte_data(client, i,
  257. PMBUS_STATUS_BYTE);
  258. for (i = 0; i < info->pages; i++) {
  259. if (!(info->func[i] & PMBUS_HAVE_STATUS_VOUT))
  260. continue;
  261. data->status[PB_STATUS_VOUT_BASE + i]
  262. = _pmbus_read_byte_data(client, i, PMBUS_STATUS_VOUT);
  263. }
  264. for (i = 0; i < info->pages; i++) {
  265. if (!(info->func[i] & PMBUS_HAVE_STATUS_IOUT))
  266. continue;
  267. data->status[PB_STATUS_IOUT_BASE + i]
  268. = _pmbus_read_byte_data(client, i, PMBUS_STATUS_IOUT);
  269. }
  270. for (i = 0; i < info->pages; i++) {
  271. if (!(info->func[i] & PMBUS_HAVE_STATUS_TEMP))
  272. continue;
  273. data->status[PB_STATUS_TEMP_BASE + i]
  274. = _pmbus_read_byte_data(client, i,
  275. PMBUS_STATUS_TEMPERATURE);
  276. }
  277. for (i = 0; i < info->pages; i++) {
  278. if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN12))
  279. continue;
  280. data->status[PB_STATUS_FAN_BASE + i]
  281. = _pmbus_read_byte_data(client, i,
  282. PMBUS_STATUS_FAN_12);
  283. }
  284. for (i = 0; i < info->pages; i++) {
  285. if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN34))
  286. continue;
  287. data->status[PB_STATUS_FAN34_BASE + i]
  288. = _pmbus_read_byte_data(client, i,
  289. PMBUS_STATUS_FAN_34);
  290. }
  291. if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
  292. data->status[PB_STATUS_INPUT_BASE]
  293. = _pmbus_read_byte_data(client, 0,
  294. PMBUS_STATUS_INPUT);
  295. for (i = 0; i < data->num_sensors; i++) {
  296. struct pmbus_sensor *sensor = &data->sensors[i];
  297. if (!data->valid || sensor->update)
  298. sensor->data
  299. = pmbus_read_word_data(client, sensor->page,
  300. sensor->reg);
  301. }
  302. pmbus_clear_faults(client);
  303. data->last_updated = jiffies;
  304. data->valid = 1;
  305. }
  306. mutex_unlock(&data->update_lock);
  307. return data;
  308. }
  309. /*
  310. * Convert linear sensor values to milli- or micro-units
  311. * depending on sensor type.
  312. */
  313. static long pmbus_reg2data_linear(struct pmbus_data *data,
  314. struct pmbus_sensor *sensor)
  315. {
  316. s16 exponent;
  317. s32 mantissa;
  318. long val;
  319. if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
  320. exponent = data->exponent;
  321. mantissa = (u16) sensor->data;
  322. } else { /* LINEAR11 */
  323. exponent = (sensor->data >> 11) & 0x001f;
  324. mantissa = sensor->data & 0x07ff;
  325. if (exponent > 0x0f)
  326. exponent |= 0xffe0; /* sign extend exponent */
  327. if (mantissa > 0x03ff)
  328. mantissa |= 0xfffff800; /* sign extend mantissa */
  329. }
  330. val = mantissa;
  331. /* scale result to milli-units for all sensors except fans */
  332. if (sensor->class != PSC_FAN)
  333. val = val * 1000L;
  334. /* scale result to micro-units for power sensors */
  335. if (sensor->class == PSC_POWER)
  336. val = val * 1000L;
  337. if (exponent >= 0)
  338. val <<= exponent;
  339. else
  340. val >>= -exponent;
  341. return val;
  342. }
  343. /*
  344. * Convert direct sensor values to milli- or micro-units
  345. * depending on sensor type.
  346. */
  347. static long pmbus_reg2data_direct(struct pmbus_data *data,
  348. struct pmbus_sensor *sensor)
  349. {
  350. long val = (s16) sensor->data;
  351. long m, b, R;
  352. m = data->info->m[sensor->class];
  353. b = data->info->b[sensor->class];
  354. R = data->info->R[sensor->class];
  355. if (m == 0)
  356. return 0;
  357. /* X = 1/m * (Y * 10^-R - b) */
  358. R = -R;
  359. /* scale result to milli-units for everything but fans */
  360. if (sensor->class != PSC_FAN) {
  361. R += 3;
  362. b *= 1000;
  363. }
  364. /* scale result to micro-units for power sensors */
  365. if (sensor->class == PSC_POWER) {
  366. R += 3;
  367. b *= 1000;
  368. }
  369. while (R > 0) {
  370. val *= 10;
  371. R--;
  372. }
  373. while (R < 0) {
  374. val = DIV_ROUND_CLOSEST(val, 10);
  375. R++;
  376. }
  377. return (val - b) / m;
  378. }
  379. static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
  380. {
  381. long val;
  382. if (data->info->direct[sensor->class])
  383. val = pmbus_reg2data_direct(data, sensor);
  384. else
  385. val = pmbus_reg2data_linear(data, sensor);
  386. return val;
  387. }
  388. #define MAX_MANTISSA (1023 * 1000)
  389. #define MIN_MANTISSA (511 * 1000)
  390. static u16 pmbus_data2reg_linear(struct pmbus_data *data,
  391. enum pmbus_sensor_classes class, long val)
  392. {
  393. s16 exponent = 0, mantissa;
  394. bool negative = false;
  395. /* simple case */
  396. if (val == 0)
  397. return 0;
  398. if (class == PSC_VOLTAGE_OUT) {
  399. /* LINEAR16 does not support negative voltages */
  400. if (val < 0)
  401. return 0;
  402. /*
  403. * For a static exponents, we don't have a choice
  404. * but to adjust the value to it.
  405. */
  406. if (data->exponent < 0)
  407. val <<= -data->exponent;
  408. else
  409. val >>= data->exponent;
  410. val = DIV_ROUND_CLOSEST(val, 1000);
  411. return val & 0xffff;
  412. }
  413. if (val < 0) {
  414. negative = true;
  415. val = -val;
  416. }
  417. /* Power is in uW. Convert to mW before converting. */
  418. if (class == PSC_POWER)
  419. val = DIV_ROUND_CLOSEST(val, 1000L);
  420. /*
  421. * For simplicity, convert fan data to milli-units
  422. * before calculating the exponent.
  423. */
  424. if (class == PSC_FAN)
  425. val = val * 1000;
  426. /* Reduce large mantissa until it fits into 10 bit */
  427. while (val >= MAX_MANTISSA && exponent < 15) {
  428. exponent++;
  429. val >>= 1;
  430. }
  431. /* Increase small mantissa to improve precision */
  432. while (val < MIN_MANTISSA && exponent > -15) {
  433. exponent--;
  434. val <<= 1;
  435. }
  436. /* Convert mantissa from milli-units to units */
  437. mantissa = DIV_ROUND_CLOSEST(val, 1000);
  438. /* Ensure that resulting number is within range */
  439. if (mantissa > 0x3ff)
  440. mantissa = 0x3ff;
  441. /* restore sign */
  442. if (negative)
  443. mantissa = -mantissa;
  444. /* Convert to 5 bit exponent, 11 bit mantissa */
  445. return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
  446. }
  447. static u16 pmbus_data2reg_direct(struct pmbus_data *data,
  448. enum pmbus_sensor_classes class, long val)
  449. {
  450. long m, b, R;
  451. m = data->info->m[class];
  452. b = data->info->b[class];
  453. R = data->info->R[class];
  454. /* Power is in uW. Adjust R and b. */
  455. if (class == PSC_POWER) {
  456. R -= 3;
  457. b *= 1000;
  458. }
  459. /* Calculate Y = (m * X + b) * 10^R */
  460. if (class != PSC_FAN) {
  461. R -= 3; /* Adjust R and b for data in milli-units */
  462. b *= 1000;
  463. }
  464. val = val * m + b;
  465. while (R > 0) {
  466. val *= 10;
  467. R--;
  468. }
  469. while (R < 0) {
  470. val = DIV_ROUND_CLOSEST(val, 10);
  471. R++;
  472. }
  473. return val;
  474. }
  475. static u16 pmbus_data2reg(struct pmbus_data *data,
  476. enum pmbus_sensor_classes class, long val)
  477. {
  478. u16 regval;
  479. if (data->info->direct[class])
  480. regval = pmbus_data2reg_direct(data, class, val);
  481. else
  482. regval = pmbus_data2reg_linear(data, class, val);
  483. return regval;
  484. }
  485. /*
  486. * Return boolean calculated from converted data.
  487. * <index> defines a status register index and mask, and optionally
  488. * two sensor indexes.
  489. * The upper half-word references the two sensors,
  490. * two sensor indices.
  491. * The upper half-word references the two optional sensors,
  492. * the lower half word references status register and mask.
  493. * The function returns true if (status[reg] & mask) is true and,
  494. * if specified, if v1 >= v2.
  495. * To determine if an object exceeds upper limits, specify <v, limit>.
  496. * To determine if an object exceeds lower limits, specify <limit, v>.
  497. *
  498. * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
  499. * index are set. s1 and s2 (the sensor index values) are zero in this case.
  500. * The function returns true if (status[reg] & mask) is true.
  501. *
  502. * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
  503. * a specified limit has to be performed to determine the boolean result.
  504. * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
  505. * sensor values referenced by sensor indices s1 and s2).
  506. *
  507. * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
  508. * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
  509. *
  510. * If a negative value is stored in any of the referenced registers, this value
  511. * reflects an error code which will be returned.
  512. */
  513. static int pmbus_get_boolean(struct pmbus_data *data, int index, int *val)
  514. {
  515. u8 s1 = (index >> 24) & 0xff;
  516. u8 s2 = (index >> 16) & 0xff;
  517. u8 reg = (index >> 8) & 0xff;
  518. u8 mask = index & 0xff;
  519. int status;
  520. u8 regval;
  521. status = data->status[reg];
  522. if (status < 0)
  523. return status;
  524. regval = status & mask;
  525. if (!s1 && !s2)
  526. *val = !!regval;
  527. else {
  528. long v1, v2;
  529. struct pmbus_sensor *sensor1, *sensor2;
  530. sensor1 = &data->sensors[s1];
  531. if (sensor1->data < 0)
  532. return sensor1->data;
  533. sensor2 = &data->sensors[s2];
  534. if (sensor2->data < 0)
  535. return sensor2->data;
  536. v1 = pmbus_reg2data(data, sensor1);
  537. v2 = pmbus_reg2data(data, sensor2);
  538. *val = !!(regval && v1 >= v2);
  539. }
  540. return 0;
  541. }
  542. static ssize_t pmbus_show_boolean(struct device *dev,
  543. struct device_attribute *da, char *buf)
  544. {
  545. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  546. struct pmbus_data *data = pmbus_update_device(dev);
  547. int val;
  548. int err;
  549. err = pmbus_get_boolean(data, attr->index, &val);
  550. if (err)
  551. return err;
  552. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  553. }
  554. static ssize_t pmbus_show_sensor(struct device *dev,
  555. struct device_attribute *da, char *buf)
  556. {
  557. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  558. struct pmbus_data *data = pmbus_update_device(dev);
  559. struct pmbus_sensor *sensor;
  560. sensor = &data->sensors[attr->index];
  561. if (sensor->data < 0)
  562. return sensor->data;
  563. return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
  564. }
  565. static ssize_t pmbus_set_sensor(struct device *dev,
  566. struct device_attribute *devattr,
  567. const char *buf, size_t count)
  568. {
  569. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  570. struct i2c_client *client = to_i2c_client(dev);
  571. struct pmbus_data *data = i2c_get_clientdata(client);
  572. struct pmbus_sensor *sensor = &data->sensors[attr->index];
  573. ssize_t rv = count;
  574. long val = 0;
  575. int ret;
  576. u16 regval;
  577. if (strict_strtol(buf, 10, &val) < 0)
  578. return -EINVAL;
  579. mutex_lock(&data->update_lock);
  580. regval = pmbus_data2reg(data, sensor->class, val);
  581. ret = pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
  582. if (ret < 0)
  583. rv = ret;
  584. else
  585. data->sensors[attr->index].data = regval;
  586. mutex_unlock(&data->update_lock);
  587. return rv;
  588. }
  589. static ssize_t pmbus_show_label(struct device *dev,
  590. struct device_attribute *da, char *buf)
  591. {
  592. struct i2c_client *client = to_i2c_client(dev);
  593. struct pmbus_data *data = i2c_get_clientdata(client);
  594. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  595. return snprintf(buf, PAGE_SIZE, "%s\n",
  596. data->labels[attr->index].label);
  597. }
  598. #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
  599. do { \
  600. struct sensor_device_attribute *a \
  601. = &data->_type##s[data->num_##_type##s].attribute; \
  602. BUG_ON(data->num_attributes >= data->max_attributes); \
  603. sysfs_attr_init(&a->dev_attr.attr); \
  604. a->dev_attr.attr.name = _name; \
  605. a->dev_attr.attr.mode = _mode; \
  606. a->dev_attr.show = _show; \
  607. a->dev_attr.store = _set; \
  608. a->index = _idx; \
  609. data->attributes[data->num_attributes] = &a->dev_attr.attr; \
  610. data->num_attributes++; \
  611. } while (0)
  612. #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
  613. PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
  614. pmbus_show_##_type, NULL)
  615. #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
  616. PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
  617. pmbus_show_##_type, pmbus_set_##_type)
  618. static void pmbus_add_boolean(struct pmbus_data *data,
  619. const char *name, const char *type, int seq,
  620. int idx)
  621. {
  622. struct pmbus_boolean *boolean;
  623. BUG_ON(data->num_booleans >= data->max_booleans);
  624. boolean = &data->booleans[data->num_booleans];
  625. snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
  626. name, seq, type);
  627. PMBUS_ADD_GET_ATTR(data, boolean->name, boolean, idx);
  628. data->num_booleans++;
  629. }
  630. static void pmbus_add_boolean_reg(struct pmbus_data *data,
  631. const char *name, const char *type,
  632. int seq, int reg, int bit)
  633. {
  634. pmbus_add_boolean(data, name, type, seq, (reg << 8) | bit);
  635. }
  636. static void pmbus_add_boolean_cmp(struct pmbus_data *data,
  637. const char *name, const char *type,
  638. int seq, int i1, int i2, int reg, int mask)
  639. {
  640. pmbus_add_boolean(data, name, type, seq,
  641. (i1 << 24) | (i2 << 16) | (reg << 8) | mask);
  642. }
  643. static void pmbus_add_sensor(struct pmbus_data *data,
  644. const char *name, const char *type, int seq,
  645. int page, int reg, enum pmbus_sensor_classes class,
  646. bool update, bool readonly)
  647. {
  648. struct pmbus_sensor *sensor;
  649. BUG_ON(data->num_sensors >= data->max_sensors);
  650. sensor = &data->sensors[data->num_sensors];
  651. snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
  652. name, seq, type);
  653. sensor->page = page;
  654. sensor->reg = reg;
  655. sensor->class = class;
  656. sensor->update = update;
  657. if (readonly)
  658. PMBUS_ADD_GET_ATTR(data, sensor->name, sensor,
  659. data->num_sensors);
  660. else
  661. PMBUS_ADD_SET_ATTR(data, sensor->name, sensor,
  662. data->num_sensors);
  663. data->num_sensors++;
  664. }
  665. static void pmbus_add_label(struct pmbus_data *data,
  666. const char *name, int seq,
  667. const char *lstring, int index)
  668. {
  669. struct pmbus_label *label;
  670. BUG_ON(data->num_labels >= data->max_labels);
  671. label = &data->labels[data->num_labels];
  672. snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
  673. if (!index)
  674. strncpy(label->label, lstring, sizeof(label->label) - 1);
  675. else
  676. snprintf(label->label, sizeof(label->label), "%s%d", lstring,
  677. index);
  678. PMBUS_ADD_GET_ATTR(data, label->name, label, data->num_labels);
  679. data->num_labels++;
  680. }
  681. /*
  682. * Determine maximum number of sensors, booleans, and labels.
  683. * To keep things simple, only make a rough high estimate.
  684. */
  685. static void pmbus_find_max_attr(struct i2c_client *client,
  686. struct pmbus_data *data)
  687. {
  688. const struct pmbus_driver_info *info = data->info;
  689. int page, max_sensors, max_booleans, max_labels;
  690. max_sensors = PMBUS_MAX_INPUT_SENSORS;
  691. max_booleans = PMBUS_MAX_INPUT_BOOLEANS;
  692. max_labels = PMBUS_MAX_INPUT_LABELS;
  693. for (page = 0; page < info->pages; page++) {
  694. if (info->func[page] & PMBUS_HAVE_VOUT) {
  695. max_sensors += PMBUS_VOUT_SENSORS_PER_PAGE;
  696. max_booleans += PMBUS_VOUT_BOOLEANS_PER_PAGE;
  697. max_labels++;
  698. }
  699. if (info->func[page] & PMBUS_HAVE_IOUT) {
  700. max_sensors += PMBUS_IOUT_SENSORS_PER_PAGE;
  701. max_booleans += PMBUS_IOUT_BOOLEANS_PER_PAGE;
  702. max_labels++;
  703. }
  704. if (info->func[page] & PMBUS_HAVE_POUT) {
  705. max_sensors += PMBUS_POUT_SENSORS_PER_PAGE;
  706. max_booleans += PMBUS_POUT_BOOLEANS_PER_PAGE;
  707. max_labels++;
  708. }
  709. if (info->func[page] & PMBUS_HAVE_FAN12) {
  710. max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
  711. max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
  712. }
  713. if (info->func[page] & PMBUS_HAVE_FAN34) {
  714. max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
  715. max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
  716. }
  717. if (info->func[page] & PMBUS_HAVE_TEMP) {
  718. max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
  719. max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
  720. }
  721. if (info->func[page] & PMBUS_HAVE_TEMP2) {
  722. max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
  723. max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
  724. }
  725. if (info->func[page] & PMBUS_HAVE_TEMP3) {
  726. max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
  727. max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
  728. }
  729. }
  730. data->max_sensors = max_sensors;
  731. data->max_booleans = max_booleans;
  732. data->max_labels = max_labels;
  733. data->max_attributes = max_sensors + max_booleans + max_labels;
  734. }
  735. /*
  736. * Search for attributes. Allocate sensors, booleans, and labels as needed.
  737. */
  738. /*
  739. * The pmbus_limit_attr structure describes a single limit attribute
  740. * and its associated alarm attribute.
  741. */
  742. struct pmbus_limit_attr {
  743. u8 reg; /* Limit register */
  744. const char *attr; /* Attribute name */
  745. const char *alarm; /* Alarm attribute name */
  746. u32 sbit; /* Alarm attribute status bit */
  747. };
  748. /*
  749. * The pmbus_sensor_attr structure describes one sensor attribute. This
  750. * description includes a reference to the associated limit attributes.
  751. */
  752. struct pmbus_sensor_attr {
  753. u8 reg; /* sensor register */
  754. enum pmbus_sensor_classes class;/* sensor class */
  755. const char *label; /* sensor label */
  756. bool paged; /* true if paged sensor */
  757. bool update; /* true if update needed */
  758. bool compare; /* true if compare function needed */
  759. u32 func; /* sensor mask */
  760. u32 sfunc; /* sensor status mask */
  761. int sbase; /* status base register */
  762. u32 gbit; /* generic status bit */
  763. const struct pmbus_limit_attr *limit;/* limit registers */
  764. int nlimit; /* # of limit registers */
  765. };
  766. /*
  767. * Add a set of limit attributes and, if supported, the associated
  768. * alarm attributes.
  769. */
  770. static bool pmbus_add_limit_attrs(struct i2c_client *client,
  771. struct pmbus_data *data,
  772. const struct pmbus_driver_info *info,
  773. const char *name, int index, int page,
  774. int cbase,
  775. const struct pmbus_sensor_attr *attr)
  776. {
  777. const struct pmbus_limit_attr *l = attr->limit;
  778. int nlimit = attr->nlimit;
  779. bool have_alarm = false;
  780. int i, cindex;
  781. for (i = 0; i < nlimit; i++) {
  782. if (pmbus_check_word_register(client, page, l->reg)) {
  783. cindex = data->num_sensors;
  784. pmbus_add_sensor(data, name, l->attr, index, page,
  785. l->reg, attr->class, attr->update,
  786. false);
  787. if (info->func[page] & attr->sfunc) {
  788. if (attr->compare) {
  789. pmbus_add_boolean_cmp(data, name,
  790. l->alarm, index,
  791. cbase, cindex,
  792. attr->sbase + page, l->sbit);
  793. } else {
  794. pmbus_add_boolean_reg(data, name,
  795. l->alarm, index,
  796. attr->sbase + page, l->sbit);
  797. }
  798. have_alarm = true;
  799. }
  800. }
  801. l++;
  802. }
  803. return have_alarm;
  804. }
  805. static void pmbus_add_sensor_attrs_one(struct i2c_client *client,
  806. struct pmbus_data *data,
  807. const struct pmbus_driver_info *info,
  808. const char *name,
  809. int index, int page,
  810. const struct pmbus_sensor_attr *attr)
  811. {
  812. bool have_alarm;
  813. int cbase = data->num_sensors;
  814. if (attr->label)
  815. pmbus_add_label(data, name, index, attr->label,
  816. attr->paged ? page + 1 : 0);
  817. pmbus_add_sensor(data, name, "input", index, page, attr->reg,
  818. attr->class, true, true);
  819. if (attr->sfunc) {
  820. have_alarm = pmbus_add_limit_attrs(client, data, info, name,
  821. index, page, cbase, attr);
  822. /*
  823. * Add generic alarm attribute only if there are no individual
  824. * alarm attributes, and if there is a global alarm bit.
  825. */
  826. if (!have_alarm && attr->gbit)
  827. pmbus_add_boolean_reg(data, name, "alarm", index,
  828. PB_STATUS_BASE + page,
  829. attr->gbit);
  830. }
  831. }
  832. static void pmbus_add_sensor_attrs(struct i2c_client *client,
  833. struct pmbus_data *data,
  834. const char *name,
  835. const struct pmbus_sensor_attr *attrs,
  836. int nattrs)
  837. {
  838. const struct pmbus_driver_info *info = data->info;
  839. int index, i;
  840. index = 1;
  841. for (i = 0; i < nattrs; i++) {
  842. int page, pages;
  843. pages = attrs->paged ? info->pages : 1;
  844. for (page = 0; page < pages; page++) {
  845. if (!(info->func[page] & attrs->func))
  846. continue;
  847. pmbus_add_sensor_attrs_one(client, data, info, name,
  848. index, page, attrs);
  849. index++;
  850. }
  851. attrs++;
  852. }
  853. }
  854. static const struct pmbus_limit_attr vin_limit_attrs[] = {
  855. {
  856. .reg = PMBUS_VIN_UV_WARN_LIMIT,
  857. .attr = "min",
  858. .alarm = "min_alarm",
  859. .sbit = PB_VOLTAGE_UV_WARNING,
  860. }, {
  861. .reg = PMBUS_VIN_UV_FAULT_LIMIT,
  862. .attr = "lcrit",
  863. .alarm = "lcrit_alarm",
  864. .sbit = PB_VOLTAGE_UV_FAULT,
  865. }, {
  866. .reg = PMBUS_VIN_OV_WARN_LIMIT,
  867. .attr = "max",
  868. .alarm = "max_alarm",
  869. .sbit = PB_VOLTAGE_OV_WARNING,
  870. }, {
  871. .reg = PMBUS_VIN_OV_FAULT_LIMIT,
  872. .attr = "crit",
  873. .alarm = "crit_alarm",
  874. .sbit = PB_VOLTAGE_OV_FAULT,
  875. },
  876. };
  877. static const struct pmbus_limit_attr vout_limit_attrs[] = {
  878. {
  879. .reg = PMBUS_VOUT_UV_WARN_LIMIT,
  880. .attr = "min",
  881. .alarm = "min_alarm",
  882. .sbit = PB_VOLTAGE_UV_WARNING,
  883. }, {
  884. .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
  885. .attr = "lcrit",
  886. .alarm = "lcrit_alarm",
  887. .sbit = PB_VOLTAGE_UV_FAULT,
  888. }, {
  889. .reg = PMBUS_VOUT_OV_WARN_LIMIT,
  890. .attr = "max",
  891. .alarm = "max_alarm",
  892. .sbit = PB_VOLTAGE_OV_WARNING,
  893. }, {
  894. .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
  895. .attr = "crit",
  896. .alarm = "crit_alarm",
  897. .sbit = PB_VOLTAGE_OV_FAULT,
  898. }
  899. };
  900. static const struct pmbus_sensor_attr voltage_attributes[] = {
  901. {
  902. .reg = PMBUS_READ_VIN,
  903. .class = PSC_VOLTAGE_IN,
  904. .label = "vin",
  905. .func = PMBUS_HAVE_VIN,
  906. .sfunc = PMBUS_HAVE_STATUS_INPUT,
  907. .sbase = PB_STATUS_INPUT_BASE,
  908. .gbit = PB_STATUS_VIN_UV,
  909. .limit = vin_limit_attrs,
  910. .nlimit = ARRAY_SIZE(vin_limit_attrs),
  911. }, {
  912. .reg = PMBUS_READ_VCAP,
  913. .class = PSC_VOLTAGE_IN,
  914. .label = "vcap",
  915. .func = PMBUS_HAVE_VCAP,
  916. }, {
  917. .reg = PMBUS_READ_VOUT,
  918. .class = PSC_VOLTAGE_OUT,
  919. .label = "vout",
  920. .paged = true,
  921. .func = PMBUS_HAVE_VOUT,
  922. .sfunc = PMBUS_HAVE_STATUS_VOUT,
  923. .sbase = PB_STATUS_VOUT_BASE,
  924. .gbit = PB_STATUS_VOUT_OV,
  925. .limit = vout_limit_attrs,
  926. .nlimit = ARRAY_SIZE(vout_limit_attrs),
  927. }
  928. };
  929. /* Current attributes */
  930. static const struct pmbus_limit_attr iin_limit_attrs[] = {
  931. {
  932. .reg = PMBUS_IIN_OC_WARN_LIMIT,
  933. .attr = "max",
  934. .alarm = "max_alarm",
  935. .sbit = PB_IIN_OC_WARNING,
  936. }, {
  937. .reg = PMBUS_IIN_OC_FAULT_LIMIT,
  938. .attr = "crit",
  939. .alarm = "crit_alarm",
  940. .sbit = PB_IIN_OC_FAULT,
  941. }
  942. };
  943. static const struct pmbus_limit_attr iout_limit_attrs[] = {
  944. {
  945. .reg = PMBUS_IOUT_OC_WARN_LIMIT,
  946. .attr = "max",
  947. .alarm = "max_alarm",
  948. .sbit = PB_IOUT_OC_WARNING,
  949. }, {
  950. .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
  951. .attr = "lcrit",
  952. .alarm = "lcrit_alarm",
  953. .sbit = PB_IOUT_UC_FAULT,
  954. }, {
  955. .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
  956. .attr = "crit",
  957. .alarm = "crit_alarm",
  958. .sbit = PB_IOUT_OC_FAULT,
  959. }
  960. };
  961. static const struct pmbus_sensor_attr current_attributes[] = {
  962. {
  963. .reg = PMBUS_READ_IIN,
  964. .class = PSC_CURRENT_IN,
  965. .label = "iin",
  966. .func = PMBUS_HAVE_IIN,
  967. .sfunc = PMBUS_HAVE_STATUS_INPUT,
  968. .sbase = PB_STATUS_INPUT_BASE,
  969. .limit = iin_limit_attrs,
  970. .nlimit = ARRAY_SIZE(iin_limit_attrs),
  971. }, {
  972. .reg = PMBUS_READ_IOUT,
  973. .class = PSC_CURRENT_OUT,
  974. .label = "iout",
  975. .paged = true,
  976. .func = PMBUS_HAVE_IOUT,
  977. .sfunc = PMBUS_HAVE_STATUS_IOUT,
  978. .sbase = PB_STATUS_IOUT_BASE,
  979. .gbit = PB_STATUS_IOUT_OC,
  980. .limit = iout_limit_attrs,
  981. .nlimit = ARRAY_SIZE(iout_limit_attrs),
  982. }
  983. };
  984. /* Power attributes */
  985. static const struct pmbus_limit_attr pin_limit_attrs[] = {
  986. {
  987. .reg = PMBUS_PIN_OP_WARN_LIMIT,
  988. .attr = "max",
  989. .alarm = "alarm",
  990. .sbit = PB_PIN_OP_WARNING,
  991. }
  992. };
  993. static const struct pmbus_limit_attr pout_limit_attrs[] = {
  994. {
  995. .reg = PMBUS_POUT_MAX,
  996. .attr = "cap",
  997. .alarm = "cap_alarm",
  998. .sbit = PB_POWER_LIMITING,
  999. }, {
  1000. .reg = PMBUS_POUT_OP_WARN_LIMIT,
  1001. .attr = "max",
  1002. .alarm = "max_alarm",
  1003. .sbit = PB_POUT_OP_WARNING,
  1004. }, {
  1005. .reg = PMBUS_POUT_OP_FAULT_LIMIT,
  1006. .attr = "crit",
  1007. .alarm = "crit_alarm",
  1008. .sbit = PB_POUT_OP_FAULT,
  1009. }
  1010. };
  1011. static const struct pmbus_sensor_attr power_attributes[] = {
  1012. {
  1013. .reg = PMBUS_READ_PIN,
  1014. .class = PSC_POWER,
  1015. .label = "pin",
  1016. .func = PMBUS_HAVE_PIN,
  1017. .sfunc = PMBUS_HAVE_STATUS_INPUT,
  1018. .sbase = PB_STATUS_INPUT_BASE,
  1019. .limit = pin_limit_attrs,
  1020. .nlimit = ARRAY_SIZE(pin_limit_attrs),
  1021. }, {
  1022. .reg = PMBUS_READ_POUT,
  1023. .class = PSC_POWER,
  1024. .label = "pout",
  1025. .paged = true,
  1026. .func = PMBUS_HAVE_POUT,
  1027. .sfunc = PMBUS_HAVE_STATUS_IOUT,
  1028. .sbase = PB_STATUS_IOUT_BASE,
  1029. .limit = pout_limit_attrs,
  1030. .nlimit = ARRAY_SIZE(pout_limit_attrs),
  1031. }
  1032. };
  1033. /* Temperature atributes */
  1034. static const struct pmbus_limit_attr temp_limit_attrs[] = {
  1035. {
  1036. .reg = PMBUS_UT_WARN_LIMIT,
  1037. .attr = "min",
  1038. .alarm = "min_alarm",
  1039. .sbit = PB_TEMP_UT_WARNING,
  1040. }, {
  1041. .reg = PMBUS_UT_FAULT_LIMIT,
  1042. .attr = "lcrit",
  1043. .alarm = "lcrit_alarm",
  1044. .sbit = PB_TEMP_UT_FAULT,
  1045. }, {
  1046. .reg = PMBUS_OT_WARN_LIMIT,
  1047. .attr = "max",
  1048. .alarm = "max_alarm",
  1049. .sbit = PB_TEMP_OT_WARNING,
  1050. }, {
  1051. .reg = PMBUS_OT_FAULT_LIMIT,
  1052. .attr = "crit",
  1053. .alarm = "crit_alarm",
  1054. .sbit = PB_TEMP_OT_FAULT,
  1055. }
  1056. };
  1057. static const struct pmbus_sensor_attr temp_attributes[] = {
  1058. {
  1059. .reg = PMBUS_READ_TEMPERATURE_1,
  1060. .class = PSC_TEMPERATURE,
  1061. .paged = true,
  1062. .update = true,
  1063. .compare = true,
  1064. .func = PMBUS_HAVE_TEMP,
  1065. .sfunc = PMBUS_HAVE_STATUS_TEMP,
  1066. .sbase = PB_STATUS_TEMP_BASE,
  1067. .gbit = PB_STATUS_TEMPERATURE,
  1068. .limit = temp_limit_attrs,
  1069. .nlimit = ARRAY_SIZE(temp_limit_attrs),
  1070. }, {
  1071. .reg = PMBUS_READ_TEMPERATURE_2,
  1072. .class = PSC_TEMPERATURE,
  1073. .paged = true,
  1074. .update = true,
  1075. .compare = true,
  1076. .func = PMBUS_HAVE_TEMP2,
  1077. .sfunc = PMBUS_HAVE_STATUS_TEMP,
  1078. .sbase = PB_STATUS_TEMP_BASE,
  1079. .gbit = PB_STATUS_TEMPERATURE,
  1080. .limit = temp_limit_attrs,
  1081. .nlimit = ARRAY_SIZE(temp_limit_attrs),
  1082. }, {
  1083. .reg = PMBUS_READ_TEMPERATURE_3,
  1084. .class = PSC_TEMPERATURE,
  1085. .paged = true,
  1086. .update = true,
  1087. .compare = true,
  1088. .func = PMBUS_HAVE_TEMP3,
  1089. .sfunc = PMBUS_HAVE_STATUS_TEMP,
  1090. .sbase = PB_STATUS_TEMP_BASE,
  1091. .gbit = PB_STATUS_TEMPERATURE,
  1092. .limit = temp_limit_attrs,
  1093. .nlimit = ARRAY_SIZE(temp_limit_attrs),
  1094. }
  1095. };
  1096. static const int pmbus_fan_registers[] = {
  1097. PMBUS_READ_FAN_SPEED_1,
  1098. PMBUS_READ_FAN_SPEED_2,
  1099. PMBUS_READ_FAN_SPEED_3,
  1100. PMBUS_READ_FAN_SPEED_4
  1101. };
  1102. static const int pmbus_fan_config_registers[] = {
  1103. PMBUS_FAN_CONFIG_12,
  1104. PMBUS_FAN_CONFIG_12,
  1105. PMBUS_FAN_CONFIG_34,
  1106. PMBUS_FAN_CONFIG_34
  1107. };
  1108. static const int pmbus_fan_status_registers[] = {
  1109. PMBUS_STATUS_FAN_12,
  1110. PMBUS_STATUS_FAN_12,
  1111. PMBUS_STATUS_FAN_34,
  1112. PMBUS_STATUS_FAN_34
  1113. };
  1114. static const u32 pmbus_fan_flags[] = {
  1115. PMBUS_HAVE_FAN12,
  1116. PMBUS_HAVE_FAN12,
  1117. PMBUS_HAVE_FAN34,
  1118. PMBUS_HAVE_FAN34
  1119. };
  1120. static const u32 pmbus_fan_status_flags[] = {
  1121. PMBUS_HAVE_STATUS_FAN12,
  1122. PMBUS_HAVE_STATUS_FAN12,
  1123. PMBUS_HAVE_STATUS_FAN34,
  1124. PMBUS_HAVE_STATUS_FAN34
  1125. };
  1126. /* Fans */
  1127. static void pmbus_add_fan_attributes(struct i2c_client *client,
  1128. struct pmbus_data *data)
  1129. {
  1130. const struct pmbus_driver_info *info = data->info;
  1131. int index = 1;
  1132. int page;
  1133. for (page = 0; page < info->pages; page++) {
  1134. int f;
  1135. for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
  1136. int regval;
  1137. if (!(info->func[page] & pmbus_fan_flags[f]))
  1138. break;
  1139. if (!pmbus_check_word_register(client, page,
  1140. pmbus_fan_registers[f]))
  1141. break;
  1142. /*
  1143. * Skip fan if not installed.
  1144. * Each fan configuration register covers multiple fans,
  1145. * so we have to do some magic.
  1146. */
  1147. regval = _pmbus_read_byte_data(client, page,
  1148. pmbus_fan_config_registers[f]);
  1149. if (regval < 0 ||
  1150. (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
  1151. continue;
  1152. pmbus_add_sensor(data, "fan", "input", index, page,
  1153. pmbus_fan_registers[f], PSC_FAN, true,
  1154. true);
  1155. /*
  1156. * Each fan status register covers multiple fans,
  1157. * so we have to do some magic.
  1158. */
  1159. if ((info->func[page] & pmbus_fan_status_flags[f]) &&
  1160. pmbus_check_byte_register(client,
  1161. page, pmbus_fan_status_registers[f])) {
  1162. int base;
  1163. if (f > 1) /* fan 3, 4 */
  1164. base = PB_STATUS_FAN34_BASE + page;
  1165. else
  1166. base = PB_STATUS_FAN_BASE + page;
  1167. pmbus_add_boolean_reg(data, "fan", "alarm",
  1168. index, base,
  1169. PB_FAN_FAN1_WARNING >> (f & 1));
  1170. pmbus_add_boolean_reg(data, "fan", "fault",
  1171. index, base,
  1172. PB_FAN_FAN1_FAULT >> (f & 1));
  1173. }
  1174. index++;
  1175. }
  1176. }
  1177. }
  1178. static void pmbus_find_attributes(struct i2c_client *client,
  1179. struct pmbus_data *data)
  1180. {
  1181. /* Voltage sensors */
  1182. pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
  1183. ARRAY_SIZE(voltage_attributes));
  1184. /* Current sensors */
  1185. pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
  1186. ARRAY_SIZE(current_attributes));
  1187. /* Power sensors */
  1188. pmbus_add_sensor_attrs(client, data, "power", power_attributes,
  1189. ARRAY_SIZE(power_attributes));
  1190. /* Temperature sensors */
  1191. pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
  1192. ARRAY_SIZE(temp_attributes));
  1193. /* Fans */
  1194. pmbus_add_fan_attributes(client, data);
  1195. }
  1196. /*
  1197. * Identify chip parameters.
  1198. * This function is called for all chips.
  1199. */
  1200. static int pmbus_identify_common(struct i2c_client *client,
  1201. struct pmbus_data *data)
  1202. {
  1203. int vout_mode = -1, exponent;
  1204. if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE))
  1205. vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
  1206. if (vout_mode >= 0 && vout_mode != 0xff) {
  1207. /*
  1208. * Not all chips support the VOUT_MODE command,
  1209. * so a failure to read it is not an error.
  1210. */
  1211. switch (vout_mode >> 5) {
  1212. case 0: /* linear mode */
  1213. if (data->info->direct[PSC_VOLTAGE_OUT])
  1214. return -ENODEV;
  1215. exponent = vout_mode & 0x1f;
  1216. /* and sign-extend it */
  1217. if (exponent & 0x10)
  1218. exponent |= ~0x1f;
  1219. data->exponent = exponent;
  1220. break;
  1221. case 2: /* direct mode */
  1222. if (!data->info->direct[PSC_VOLTAGE_OUT])
  1223. return -ENODEV;
  1224. break;
  1225. default:
  1226. return -ENODEV;
  1227. }
  1228. }
  1229. /* Determine maximum number of sensors, booleans, and labels */
  1230. pmbus_find_max_attr(client, data);
  1231. pmbus_clear_fault_page(client, 0);
  1232. return 0;
  1233. }
  1234. int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
  1235. struct pmbus_driver_info *info)
  1236. {
  1237. const struct pmbus_platform_data *pdata = client->dev.platform_data;
  1238. struct pmbus_data *data;
  1239. int ret;
  1240. if (!info) {
  1241. dev_err(&client->dev, "Missing chip information");
  1242. return -ENODEV;
  1243. }
  1244. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
  1245. | I2C_FUNC_SMBUS_BYTE_DATA
  1246. | I2C_FUNC_SMBUS_WORD_DATA))
  1247. return -ENODEV;
  1248. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1249. if (!data) {
  1250. dev_err(&client->dev, "No memory to allocate driver data\n");
  1251. return -ENOMEM;
  1252. }
  1253. i2c_set_clientdata(client, data);
  1254. mutex_init(&data->update_lock);
  1255. /* Bail out if PMBus status register does not exist. */
  1256. if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0) {
  1257. dev_err(&client->dev, "PMBus status register not found\n");
  1258. ret = -ENODEV;
  1259. goto out_data;
  1260. }
  1261. if (pdata)
  1262. data->flags = pdata->flags;
  1263. data->info = info;
  1264. pmbus_clear_faults(client);
  1265. if (info->identify) {
  1266. ret = (*info->identify)(client, info);
  1267. if (ret < 0) {
  1268. dev_err(&client->dev, "Chip identification failed\n");
  1269. goto out_data;
  1270. }
  1271. }
  1272. if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
  1273. dev_err(&client->dev, "Bad number of PMBus pages: %d\n",
  1274. info->pages);
  1275. ret = -EINVAL;
  1276. goto out_data;
  1277. }
  1278. /*
  1279. * Bail out if more than one page was configured, but we can not
  1280. * select the highest page. This is an indication that the wrong
  1281. * chip type was selected. Better bail out now than keep
  1282. * returning errors later on.
  1283. */
  1284. if (info->pages > 1 && pmbus_set_page(client, info->pages - 1) < 0) {
  1285. dev_err(&client->dev, "Failed to select page %d\n",
  1286. info->pages - 1);
  1287. ret = -EINVAL;
  1288. goto out_data;
  1289. }
  1290. ret = pmbus_identify_common(client, data);
  1291. if (ret < 0) {
  1292. dev_err(&client->dev, "Failed to identify chip capabilities\n");
  1293. goto out_data;
  1294. }
  1295. ret = -ENOMEM;
  1296. data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors,
  1297. GFP_KERNEL);
  1298. if (!data->sensors) {
  1299. dev_err(&client->dev, "No memory to allocate sensor data\n");
  1300. goto out_data;
  1301. }
  1302. data->booleans = kzalloc(sizeof(struct pmbus_boolean)
  1303. * data->max_booleans, GFP_KERNEL);
  1304. if (!data->booleans) {
  1305. dev_err(&client->dev, "No memory to allocate boolean data\n");
  1306. goto out_sensors;
  1307. }
  1308. data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels,
  1309. GFP_KERNEL);
  1310. if (!data->labels) {
  1311. dev_err(&client->dev, "No memory to allocate label data\n");
  1312. goto out_booleans;
  1313. }
  1314. data->attributes = kzalloc(sizeof(struct attribute *)
  1315. * data->max_attributes, GFP_KERNEL);
  1316. if (!data->attributes) {
  1317. dev_err(&client->dev, "No memory to allocate attribute data\n");
  1318. goto out_labels;
  1319. }
  1320. pmbus_find_attributes(client, data);
  1321. /*
  1322. * If there are no attributes, something is wrong.
  1323. * Bail out instead of trying to register nothing.
  1324. */
  1325. if (!data->num_attributes) {
  1326. dev_err(&client->dev, "No attributes found\n");
  1327. ret = -ENODEV;
  1328. goto out_attributes;
  1329. }
  1330. /* Register sysfs hooks */
  1331. data->group.attrs = data->attributes;
  1332. ret = sysfs_create_group(&client->dev.kobj, &data->group);
  1333. if (ret) {
  1334. dev_err(&client->dev, "Failed to create sysfs entries\n");
  1335. goto out_attributes;
  1336. }
  1337. data->hwmon_dev = hwmon_device_register(&client->dev);
  1338. if (IS_ERR(data->hwmon_dev)) {
  1339. ret = PTR_ERR(data->hwmon_dev);
  1340. dev_err(&client->dev, "Failed to register hwmon device\n");
  1341. goto out_hwmon_device_register;
  1342. }
  1343. return 0;
  1344. out_hwmon_device_register:
  1345. sysfs_remove_group(&client->dev.kobj, &data->group);
  1346. out_attributes:
  1347. kfree(data->attributes);
  1348. out_labels:
  1349. kfree(data->labels);
  1350. out_booleans:
  1351. kfree(data->booleans);
  1352. out_sensors:
  1353. kfree(data->sensors);
  1354. out_data:
  1355. kfree(data);
  1356. return ret;
  1357. }
  1358. EXPORT_SYMBOL_GPL(pmbus_do_probe);
  1359. int pmbus_do_remove(struct i2c_client *client)
  1360. {
  1361. struct pmbus_data *data = i2c_get_clientdata(client);
  1362. hwmon_device_unregister(data->hwmon_dev);
  1363. sysfs_remove_group(&client->dev.kobj, &data->group);
  1364. kfree(data->attributes);
  1365. kfree(data->labels);
  1366. kfree(data->booleans);
  1367. kfree(data->sensors);
  1368. kfree(data);
  1369. return 0;
  1370. }
  1371. EXPORT_SYMBOL_GPL(pmbus_do_remove);
  1372. MODULE_AUTHOR("Guenter Roeck");
  1373. MODULE_DESCRIPTION("PMBus core driver");
  1374. MODULE_LICENSE("GPL");