pmbus_core.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. /*
  2. * Hardware monitoring driver for PMBus devices
  3. *
  4. * Copyright (c) 2010, 2011 Ericsson AB.
  5. * Copyright (c) 2012 Guenter Roeck
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/debugfs.h>
  22. #include <linux/kernel.h>
  23. #include <linux/math64.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/err.h>
  27. #include <linux/slab.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/pmbus.h>
  33. #include <linux/regulator/driver.h>
  34. #include <linux/regulator/machine.h>
  35. #include "pmbus.h"
  36. /*
  37. * Number of additional attribute pointers to allocate
  38. * with each call to krealloc
  39. */
  40. #define PMBUS_ATTR_ALLOC_SIZE 32
  41. /*
  42. * Index into status register array, per status register group
  43. */
  44. #define PB_STATUS_BASE 0
  45. #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
  46. #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
  47. #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
  48. #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
  49. #define PB_STATUS_TEMP_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
  50. #define PB_STATUS_INPUT_BASE (PB_STATUS_TEMP_BASE + PMBUS_PAGES)
  51. #define PB_STATUS_VMON_BASE (PB_STATUS_INPUT_BASE + 1)
  52. #define PB_NUM_STATUS_REG (PB_STATUS_VMON_BASE + 1)
  53. #define PMBUS_NAME_SIZE 24
  54. struct pmbus_sensor {
  55. struct pmbus_sensor *next;
  56. char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */
  57. struct device_attribute attribute;
  58. u8 page; /* page number */
  59. u16 reg; /* register */
  60. enum pmbus_sensor_classes class; /* sensor class */
  61. bool update; /* runtime sensor update needed */
  62. int data; /* Sensor data.
  63. Negative if there was a read error */
  64. };
  65. #define to_pmbus_sensor(_attr) \
  66. container_of(_attr, struct pmbus_sensor, attribute)
  67. struct pmbus_boolean {
  68. char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */
  69. struct sensor_device_attribute attribute;
  70. struct pmbus_sensor *s1;
  71. struct pmbus_sensor *s2;
  72. };
  73. #define to_pmbus_boolean(_attr) \
  74. container_of(_attr, struct pmbus_boolean, attribute)
  75. struct pmbus_label {
  76. char name[PMBUS_NAME_SIZE]; /* sysfs label name */
  77. struct device_attribute attribute;
  78. char label[PMBUS_NAME_SIZE]; /* label */
  79. };
  80. #define to_pmbus_label(_attr) \
  81. container_of(_attr, struct pmbus_label, attribute)
  82. struct pmbus_data {
  83. struct device *dev;
  84. struct device *hwmon_dev;
  85. u32 flags; /* from platform data */
  86. int exponent[PMBUS_PAGES];
  87. /* linear mode: exponent for output voltages */
  88. const struct pmbus_driver_info *info;
  89. int max_attributes;
  90. int num_attributes;
  91. struct attribute_group group;
  92. const struct attribute_group *groups[2];
  93. struct dentry *debugfs; /* debugfs device directory */
  94. struct pmbus_sensor *sensors;
  95. struct mutex update_lock;
  96. bool valid;
  97. unsigned long last_updated; /* in jiffies */
  98. /*
  99. * A single status register covers multiple attributes,
  100. * so we keep them all together.
  101. */
  102. u16 status[PB_NUM_STATUS_REG];
  103. bool has_status_word; /* device uses STATUS_WORD register */
  104. int (*read_status)(struct i2c_client *client, int page);
  105. u8 currpage;
  106. };
  107. struct pmbus_debugfs_entry {
  108. struct i2c_client *client;
  109. u8 page;
  110. u8 reg;
  111. };
  112. void pmbus_clear_cache(struct i2c_client *client)
  113. {
  114. struct pmbus_data *data = i2c_get_clientdata(client);
  115. data->valid = false;
  116. }
  117. EXPORT_SYMBOL_GPL(pmbus_clear_cache);
  118. int pmbus_set_page(struct i2c_client *client, int page)
  119. {
  120. struct pmbus_data *data = i2c_get_clientdata(client);
  121. int rv = 0;
  122. int newpage;
  123. if (page >= 0 && page != data->currpage) {
  124. rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
  125. newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
  126. if (newpage != page)
  127. rv = -EIO;
  128. else
  129. data->currpage = page;
  130. }
  131. return rv;
  132. }
  133. EXPORT_SYMBOL_GPL(pmbus_set_page);
  134. int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
  135. {
  136. int rv;
  137. rv = pmbus_set_page(client, page);
  138. if (rv < 0)
  139. return rv;
  140. return i2c_smbus_write_byte(client, value);
  141. }
  142. EXPORT_SYMBOL_GPL(pmbus_write_byte);
  143. /*
  144. * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
  145. * a device specific mapping function exists and calls it if necessary.
  146. */
  147. static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
  148. {
  149. struct pmbus_data *data = i2c_get_clientdata(client);
  150. const struct pmbus_driver_info *info = data->info;
  151. int status;
  152. if (info->write_byte) {
  153. status = info->write_byte(client, page, value);
  154. if (status != -ENODATA)
  155. return status;
  156. }
  157. return pmbus_write_byte(client, page, value);
  158. }
  159. int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg,
  160. u16 word)
  161. {
  162. int rv;
  163. rv = pmbus_set_page(client, page);
  164. if (rv < 0)
  165. return rv;
  166. return i2c_smbus_write_word_data(client, reg, word);
  167. }
  168. EXPORT_SYMBOL_GPL(pmbus_write_word_data);
  169. /*
  170. * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
  171. * a device specific mapping function exists and calls it if necessary.
  172. */
  173. static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
  174. u16 word)
  175. {
  176. struct pmbus_data *data = i2c_get_clientdata(client);
  177. const struct pmbus_driver_info *info = data->info;
  178. int status;
  179. if (info->write_word_data) {
  180. status = info->write_word_data(client, page, reg, word);
  181. if (status != -ENODATA)
  182. return status;
  183. }
  184. if (reg >= PMBUS_VIRT_BASE)
  185. return -ENXIO;
  186. return pmbus_write_word_data(client, page, reg, word);
  187. }
  188. int pmbus_read_word_data(struct i2c_client *client, int page, u8 reg)
  189. {
  190. int rv;
  191. rv = pmbus_set_page(client, page);
  192. if (rv < 0)
  193. return rv;
  194. return i2c_smbus_read_word_data(client, reg);
  195. }
  196. EXPORT_SYMBOL_GPL(pmbus_read_word_data);
  197. /*
  198. * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
  199. * a device specific mapping function exists and calls it if necessary.
  200. */
  201. static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg)
  202. {
  203. struct pmbus_data *data = i2c_get_clientdata(client);
  204. const struct pmbus_driver_info *info = data->info;
  205. int status;
  206. if (info->read_word_data) {
  207. status = info->read_word_data(client, page, reg);
  208. if (status != -ENODATA)
  209. return status;
  210. }
  211. if (reg >= PMBUS_VIRT_BASE)
  212. return -ENXIO;
  213. return pmbus_read_word_data(client, page, reg);
  214. }
  215. int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
  216. {
  217. int rv;
  218. rv = pmbus_set_page(client, page);
  219. if (rv < 0)
  220. return rv;
  221. return i2c_smbus_read_byte_data(client, reg);
  222. }
  223. EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
  224. int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value)
  225. {
  226. int rv;
  227. rv = pmbus_set_page(client, page);
  228. if (rv < 0)
  229. return rv;
  230. return i2c_smbus_write_byte_data(client, reg, value);
  231. }
  232. EXPORT_SYMBOL_GPL(pmbus_write_byte_data);
  233. int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg,
  234. u8 mask, u8 value)
  235. {
  236. unsigned int tmp;
  237. int rv;
  238. rv = pmbus_read_byte_data(client, page, reg);
  239. if (rv < 0)
  240. return rv;
  241. tmp = (rv & ~mask) | (value & mask);
  242. if (tmp != rv)
  243. rv = pmbus_write_byte_data(client, page, reg, tmp);
  244. return rv;
  245. }
  246. EXPORT_SYMBOL_GPL(pmbus_update_byte_data);
  247. /*
  248. * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
  249. * a device specific mapping function exists and calls it if necessary.
  250. */
  251. static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
  252. {
  253. struct pmbus_data *data = i2c_get_clientdata(client);
  254. const struct pmbus_driver_info *info = data->info;
  255. int status;
  256. if (info->read_byte_data) {
  257. status = info->read_byte_data(client, page, reg);
  258. if (status != -ENODATA)
  259. return status;
  260. }
  261. return pmbus_read_byte_data(client, page, reg);
  262. }
  263. static void pmbus_clear_fault_page(struct i2c_client *client, int page)
  264. {
  265. _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
  266. }
  267. void pmbus_clear_faults(struct i2c_client *client)
  268. {
  269. struct pmbus_data *data = i2c_get_clientdata(client);
  270. int i;
  271. for (i = 0; i < data->info->pages; i++)
  272. pmbus_clear_fault_page(client, i);
  273. }
  274. EXPORT_SYMBOL_GPL(pmbus_clear_faults);
  275. static int pmbus_check_status_cml(struct i2c_client *client)
  276. {
  277. struct pmbus_data *data = i2c_get_clientdata(client);
  278. int status, status2;
  279. status = data->read_status(client, -1);
  280. if (status < 0 || (status & PB_STATUS_CML)) {
  281. status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
  282. if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
  283. return -EIO;
  284. }
  285. return 0;
  286. }
  287. static bool pmbus_check_register(struct i2c_client *client,
  288. int (*func)(struct i2c_client *client,
  289. int page, int reg),
  290. int page, int reg)
  291. {
  292. int rv;
  293. struct pmbus_data *data = i2c_get_clientdata(client);
  294. rv = func(client, page, reg);
  295. if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
  296. rv = pmbus_check_status_cml(client);
  297. pmbus_clear_fault_page(client, -1);
  298. return rv >= 0;
  299. }
  300. static bool pmbus_check_status_register(struct i2c_client *client, int page)
  301. {
  302. int status;
  303. struct pmbus_data *data = i2c_get_clientdata(client);
  304. status = data->read_status(client, page);
  305. if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) &&
  306. (status & PB_STATUS_CML)) {
  307. status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
  308. if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND))
  309. status = -EIO;
  310. }
  311. pmbus_clear_fault_page(client, -1);
  312. return status >= 0;
  313. }
  314. bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
  315. {
  316. return pmbus_check_register(client, _pmbus_read_byte_data, page, reg);
  317. }
  318. EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
  319. bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
  320. {
  321. return pmbus_check_register(client, _pmbus_read_word_data, page, reg);
  322. }
  323. EXPORT_SYMBOL_GPL(pmbus_check_word_register);
  324. const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
  325. {
  326. struct pmbus_data *data = i2c_get_clientdata(client);
  327. return data->info;
  328. }
  329. EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
  330. static struct _pmbus_status {
  331. u32 func;
  332. u16 base;
  333. u16 reg;
  334. } pmbus_status[] = {
  335. { PMBUS_HAVE_STATUS_VOUT, PB_STATUS_VOUT_BASE, PMBUS_STATUS_VOUT },
  336. { PMBUS_HAVE_STATUS_IOUT, PB_STATUS_IOUT_BASE, PMBUS_STATUS_IOUT },
  337. { PMBUS_HAVE_STATUS_TEMP, PB_STATUS_TEMP_BASE,
  338. PMBUS_STATUS_TEMPERATURE },
  339. { PMBUS_HAVE_STATUS_FAN12, PB_STATUS_FAN_BASE, PMBUS_STATUS_FAN_12 },
  340. { PMBUS_HAVE_STATUS_FAN34, PB_STATUS_FAN34_BASE, PMBUS_STATUS_FAN_34 },
  341. };
  342. static struct pmbus_data *pmbus_update_device(struct device *dev)
  343. {
  344. struct i2c_client *client = to_i2c_client(dev->parent);
  345. struct pmbus_data *data = i2c_get_clientdata(client);
  346. const struct pmbus_driver_info *info = data->info;
  347. struct pmbus_sensor *sensor;
  348. mutex_lock(&data->update_lock);
  349. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  350. int i, j;
  351. for (i = 0; i < info->pages; i++) {
  352. data->status[PB_STATUS_BASE + i]
  353. = data->read_status(client, i);
  354. for (j = 0; j < ARRAY_SIZE(pmbus_status); j++) {
  355. struct _pmbus_status *s = &pmbus_status[j];
  356. if (!(info->func[i] & s->func))
  357. continue;
  358. data->status[s->base + i]
  359. = _pmbus_read_byte_data(client, i,
  360. s->reg);
  361. }
  362. }
  363. if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
  364. data->status[PB_STATUS_INPUT_BASE]
  365. = _pmbus_read_byte_data(client, 0,
  366. PMBUS_STATUS_INPUT);
  367. if (info->func[0] & PMBUS_HAVE_STATUS_VMON)
  368. data->status[PB_STATUS_VMON_BASE]
  369. = _pmbus_read_byte_data(client, 0,
  370. PMBUS_VIRT_STATUS_VMON);
  371. for (sensor = data->sensors; sensor; sensor = sensor->next) {
  372. if (!data->valid || sensor->update)
  373. sensor->data
  374. = _pmbus_read_word_data(client,
  375. sensor->page,
  376. sensor->reg);
  377. }
  378. pmbus_clear_faults(client);
  379. data->last_updated = jiffies;
  380. data->valid = 1;
  381. }
  382. mutex_unlock(&data->update_lock);
  383. return data;
  384. }
  385. /*
  386. * Convert linear sensor values to milli- or micro-units
  387. * depending on sensor type.
  388. */
  389. static long pmbus_reg2data_linear(struct pmbus_data *data,
  390. struct pmbus_sensor *sensor)
  391. {
  392. s16 exponent;
  393. s32 mantissa;
  394. long val;
  395. if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
  396. exponent = data->exponent[sensor->page];
  397. mantissa = (u16) sensor->data;
  398. } else { /* LINEAR11 */
  399. exponent = ((s16)sensor->data) >> 11;
  400. mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
  401. }
  402. val = mantissa;
  403. /* scale result to milli-units for all sensors except fans */
  404. if (sensor->class != PSC_FAN)
  405. val = val * 1000L;
  406. /* scale result to micro-units for power sensors */
  407. if (sensor->class == PSC_POWER)
  408. val = val * 1000L;
  409. if (exponent >= 0)
  410. val <<= exponent;
  411. else
  412. val >>= -exponent;
  413. return val;
  414. }
  415. /*
  416. * Convert direct sensor values to milli- or micro-units
  417. * depending on sensor type.
  418. */
  419. static long pmbus_reg2data_direct(struct pmbus_data *data,
  420. struct pmbus_sensor *sensor)
  421. {
  422. s64 b, val = (s16)sensor->data;
  423. s32 m, R;
  424. m = data->info->m[sensor->class];
  425. b = data->info->b[sensor->class];
  426. R = data->info->R[sensor->class];
  427. if (m == 0)
  428. return 0;
  429. /* X = 1/m * (Y * 10^-R - b) */
  430. R = -R;
  431. /* scale result to milli-units for everything but fans */
  432. if (sensor->class != PSC_FAN) {
  433. R += 3;
  434. b *= 1000;
  435. }
  436. /* scale result to micro-units for power sensors */
  437. if (sensor->class == PSC_POWER) {
  438. R += 3;
  439. b *= 1000;
  440. }
  441. while (R > 0) {
  442. val *= 10;
  443. R--;
  444. }
  445. while (R < 0) {
  446. val = div_s64(val + 5LL, 10L); /* round closest */
  447. R++;
  448. }
  449. val = div_s64(val - b, m);
  450. return clamp_val(val, LONG_MIN, LONG_MAX);
  451. }
  452. /*
  453. * Convert VID sensor values to milli- or micro-units
  454. * depending on sensor type.
  455. */
  456. static long pmbus_reg2data_vid(struct pmbus_data *data,
  457. struct pmbus_sensor *sensor)
  458. {
  459. long val = sensor->data;
  460. long rv = 0;
  461. switch (data->info->vrm_version) {
  462. case vr11:
  463. if (val >= 0x02 && val <= 0xb2)
  464. rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
  465. break;
  466. case vr12:
  467. if (val >= 0x01)
  468. rv = 250 + (val - 1) * 5;
  469. break;
  470. case vr13:
  471. if (val >= 0x01)
  472. rv = 500 + (val - 1) * 10;
  473. break;
  474. }
  475. return rv;
  476. }
  477. static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
  478. {
  479. long val;
  480. switch (data->info->format[sensor->class]) {
  481. case direct:
  482. val = pmbus_reg2data_direct(data, sensor);
  483. break;
  484. case vid:
  485. val = pmbus_reg2data_vid(data, sensor);
  486. break;
  487. case linear:
  488. default:
  489. val = pmbus_reg2data_linear(data, sensor);
  490. break;
  491. }
  492. return val;
  493. }
  494. #define MAX_MANTISSA (1023 * 1000)
  495. #define MIN_MANTISSA (511 * 1000)
  496. static u16 pmbus_data2reg_linear(struct pmbus_data *data,
  497. struct pmbus_sensor *sensor, long val)
  498. {
  499. s16 exponent = 0, mantissa;
  500. bool negative = false;
  501. /* simple case */
  502. if (val == 0)
  503. return 0;
  504. if (sensor->class == PSC_VOLTAGE_OUT) {
  505. /* LINEAR16 does not support negative voltages */
  506. if (val < 0)
  507. return 0;
  508. /*
  509. * For a static exponents, we don't have a choice
  510. * but to adjust the value to it.
  511. */
  512. if (data->exponent[sensor->page] < 0)
  513. val <<= -data->exponent[sensor->page];
  514. else
  515. val >>= data->exponent[sensor->page];
  516. val = DIV_ROUND_CLOSEST(val, 1000);
  517. return val & 0xffff;
  518. }
  519. if (val < 0) {
  520. negative = true;
  521. val = -val;
  522. }
  523. /* Power is in uW. Convert to mW before converting. */
  524. if (sensor->class == PSC_POWER)
  525. val = DIV_ROUND_CLOSEST(val, 1000L);
  526. /*
  527. * For simplicity, convert fan data to milli-units
  528. * before calculating the exponent.
  529. */
  530. if (sensor->class == PSC_FAN)
  531. val = val * 1000;
  532. /* Reduce large mantissa until it fits into 10 bit */
  533. while (val >= MAX_MANTISSA && exponent < 15) {
  534. exponent++;
  535. val >>= 1;
  536. }
  537. /* Increase small mantissa to improve precision */
  538. while (val < MIN_MANTISSA && exponent > -15) {
  539. exponent--;
  540. val <<= 1;
  541. }
  542. /* Convert mantissa from milli-units to units */
  543. mantissa = DIV_ROUND_CLOSEST(val, 1000);
  544. /* Ensure that resulting number is within range */
  545. if (mantissa > 0x3ff)
  546. mantissa = 0x3ff;
  547. /* restore sign */
  548. if (negative)
  549. mantissa = -mantissa;
  550. /* Convert to 5 bit exponent, 11 bit mantissa */
  551. return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
  552. }
  553. static u16 pmbus_data2reg_direct(struct pmbus_data *data,
  554. struct pmbus_sensor *sensor, long val)
  555. {
  556. s64 b, val64 = val;
  557. s32 m, R;
  558. m = data->info->m[sensor->class];
  559. b = data->info->b[sensor->class];
  560. R = data->info->R[sensor->class];
  561. /* Power is in uW. Adjust R and b. */
  562. if (sensor->class == PSC_POWER) {
  563. R -= 3;
  564. b *= 1000;
  565. }
  566. /* Calculate Y = (m * X + b) * 10^R */
  567. if (sensor->class != PSC_FAN) {
  568. R -= 3; /* Adjust R and b for data in milli-units */
  569. b *= 1000;
  570. }
  571. val64 = val64 * m + b;
  572. while (R > 0) {
  573. val64 *= 10;
  574. R--;
  575. }
  576. while (R < 0) {
  577. val64 = div_s64(val64 + 5LL, 10L); /* round closest */
  578. R++;
  579. }
  580. return (u16)clamp_val(val64, S16_MIN, S16_MAX);
  581. }
  582. static u16 pmbus_data2reg_vid(struct pmbus_data *data,
  583. struct pmbus_sensor *sensor, long val)
  584. {
  585. val = clamp_val(val, 500, 1600);
  586. return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625);
  587. }
  588. static u16 pmbus_data2reg(struct pmbus_data *data,
  589. struct pmbus_sensor *sensor, long val)
  590. {
  591. u16 regval;
  592. switch (data->info->format[sensor->class]) {
  593. case direct:
  594. regval = pmbus_data2reg_direct(data, sensor, val);
  595. break;
  596. case vid:
  597. regval = pmbus_data2reg_vid(data, sensor, val);
  598. break;
  599. case linear:
  600. default:
  601. regval = pmbus_data2reg_linear(data, sensor, val);
  602. break;
  603. }
  604. return regval;
  605. }
  606. /*
  607. * Return boolean calculated from converted data.
  608. * <index> defines a status register index and mask.
  609. * The mask is in the lower 8 bits, the register index is in bits 8..23.
  610. *
  611. * The associated pmbus_boolean structure contains optional pointers to two
  612. * sensor attributes. If specified, those attributes are compared against each
  613. * other to determine if a limit has been exceeded.
  614. *
  615. * If the sensor attribute pointers are NULL, the function returns true if
  616. * (status[reg] & mask) is true.
  617. *
  618. * If sensor attribute pointers are provided, a comparison against a specified
  619. * limit has to be performed to determine the boolean result.
  620. * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
  621. * sensor values referenced by sensor attribute pointers s1 and s2).
  622. *
  623. * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
  624. * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
  625. *
  626. * If a negative value is stored in any of the referenced registers, this value
  627. * reflects an error code which will be returned.
  628. */
  629. static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b,
  630. int index)
  631. {
  632. struct pmbus_sensor *s1 = b->s1;
  633. struct pmbus_sensor *s2 = b->s2;
  634. u16 reg = (index >> 16) & 0xffff;
  635. u16 mask = index & 0xffff;
  636. int ret, status;
  637. u16 regval;
  638. status = data->status[reg];
  639. if (status < 0)
  640. return status;
  641. regval = status & mask;
  642. if (!s1 && !s2) {
  643. ret = !!regval;
  644. } else if (!s1 || !s2) {
  645. WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2);
  646. return 0;
  647. } else {
  648. long v1, v2;
  649. if (s1->data < 0)
  650. return s1->data;
  651. if (s2->data < 0)
  652. return s2->data;
  653. v1 = pmbus_reg2data(data, s1);
  654. v2 = pmbus_reg2data(data, s2);
  655. ret = !!(regval && v1 >= v2);
  656. }
  657. return ret;
  658. }
  659. static ssize_t pmbus_show_boolean(struct device *dev,
  660. struct device_attribute *da, char *buf)
  661. {
  662. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  663. struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
  664. struct pmbus_data *data = pmbus_update_device(dev);
  665. int val;
  666. val = pmbus_get_boolean(data, boolean, attr->index);
  667. if (val < 0)
  668. return val;
  669. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  670. }
  671. static ssize_t pmbus_show_sensor(struct device *dev,
  672. struct device_attribute *devattr, char *buf)
  673. {
  674. struct pmbus_data *data = pmbus_update_device(dev);
  675. struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
  676. if (sensor->data < 0)
  677. return sensor->data;
  678. return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
  679. }
  680. static ssize_t pmbus_set_sensor(struct device *dev,
  681. struct device_attribute *devattr,
  682. const char *buf, size_t count)
  683. {
  684. struct i2c_client *client = to_i2c_client(dev->parent);
  685. struct pmbus_data *data = i2c_get_clientdata(client);
  686. struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
  687. ssize_t rv = count;
  688. long val = 0;
  689. int ret;
  690. u16 regval;
  691. if (kstrtol(buf, 10, &val) < 0)
  692. return -EINVAL;
  693. mutex_lock(&data->update_lock);
  694. regval = pmbus_data2reg(data, sensor, val);
  695. ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
  696. if (ret < 0)
  697. rv = ret;
  698. else
  699. sensor->data = regval;
  700. mutex_unlock(&data->update_lock);
  701. return rv;
  702. }
  703. static ssize_t pmbus_show_label(struct device *dev,
  704. struct device_attribute *da, char *buf)
  705. {
  706. struct pmbus_label *label = to_pmbus_label(da);
  707. return snprintf(buf, PAGE_SIZE, "%s\n", label->label);
  708. }
  709. static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
  710. {
  711. if (data->num_attributes >= data->max_attributes - 1) {
  712. int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
  713. void *new_attrs = krealloc(data->group.attrs,
  714. new_max_attrs * sizeof(void *),
  715. GFP_KERNEL);
  716. if (!new_attrs)
  717. return -ENOMEM;
  718. data->group.attrs = new_attrs;
  719. data->max_attributes = new_max_attrs;
  720. }
  721. data->group.attrs[data->num_attributes++] = attr;
  722. data->group.attrs[data->num_attributes] = NULL;
  723. return 0;
  724. }
  725. static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
  726. const char *name,
  727. umode_t mode,
  728. ssize_t (*show)(struct device *dev,
  729. struct device_attribute *attr,
  730. char *buf),
  731. ssize_t (*store)(struct device *dev,
  732. struct device_attribute *attr,
  733. const char *buf, size_t count))
  734. {
  735. sysfs_attr_init(&dev_attr->attr);
  736. dev_attr->attr.name = name;
  737. dev_attr->attr.mode = mode;
  738. dev_attr->show = show;
  739. dev_attr->store = store;
  740. }
  741. static void pmbus_attr_init(struct sensor_device_attribute *a,
  742. const char *name,
  743. umode_t mode,
  744. ssize_t (*show)(struct device *dev,
  745. struct device_attribute *attr,
  746. char *buf),
  747. ssize_t (*store)(struct device *dev,
  748. struct device_attribute *attr,
  749. const char *buf, size_t count),
  750. int idx)
  751. {
  752. pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
  753. a->index = idx;
  754. }
  755. static int pmbus_add_boolean(struct pmbus_data *data,
  756. const char *name, const char *type, int seq,
  757. struct pmbus_sensor *s1,
  758. struct pmbus_sensor *s2,
  759. u16 reg, u16 mask)
  760. {
  761. struct pmbus_boolean *boolean;
  762. struct sensor_device_attribute *a;
  763. boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
  764. if (!boolean)
  765. return -ENOMEM;
  766. a = &boolean->attribute;
  767. snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
  768. name, seq, type);
  769. boolean->s1 = s1;
  770. boolean->s2 = s2;
  771. pmbus_attr_init(a, boolean->name, S_IRUGO, pmbus_show_boolean, NULL,
  772. (reg << 16) | mask);
  773. return pmbus_add_attribute(data, &a->dev_attr.attr);
  774. }
  775. static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
  776. const char *name, const char *type,
  777. int seq, int page, int reg,
  778. enum pmbus_sensor_classes class,
  779. bool update, bool readonly)
  780. {
  781. struct pmbus_sensor *sensor;
  782. struct device_attribute *a;
  783. sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
  784. if (!sensor)
  785. return NULL;
  786. a = &sensor->attribute;
  787. snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
  788. name, seq, type);
  789. sensor->page = page;
  790. sensor->reg = reg;
  791. sensor->class = class;
  792. sensor->update = update;
  793. pmbus_dev_attr_init(a, sensor->name,
  794. readonly ? S_IRUGO : S_IRUGO | S_IWUSR,
  795. pmbus_show_sensor, pmbus_set_sensor);
  796. if (pmbus_add_attribute(data, &a->attr))
  797. return NULL;
  798. sensor->next = data->sensors;
  799. data->sensors = sensor;
  800. return sensor;
  801. }
  802. static int pmbus_add_label(struct pmbus_data *data,
  803. const char *name, int seq,
  804. const char *lstring, int index)
  805. {
  806. struct pmbus_label *label;
  807. struct device_attribute *a;
  808. label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
  809. if (!label)
  810. return -ENOMEM;
  811. a = &label->attribute;
  812. snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
  813. if (!index)
  814. strncpy(label->label, lstring, sizeof(label->label) - 1);
  815. else
  816. snprintf(label->label, sizeof(label->label), "%s%d", lstring,
  817. index);
  818. pmbus_dev_attr_init(a, label->name, S_IRUGO, pmbus_show_label, NULL);
  819. return pmbus_add_attribute(data, &a->attr);
  820. }
  821. /*
  822. * Search for attributes. Allocate sensors, booleans, and labels as needed.
  823. */
  824. /*
  825. * The pmbus_limit_attr structure describes a single limit attribute
  826. * and its associated alarm attribute.
  827. */
  828. struct pmbus_limit_attr {
  829. u16 reg; /* Limit register */
  830. u16 sbit; /* Alarm attribute status bit */
  831. bool update; /* True if register needs updates */
  832. bool low; /* True if low limit; for limits with compare
  833. functions only */
  834. const char *attr; /* Attribute name */
  835. const char *alarm; /* Alarm attribute name */
  836. };
  837. /*
  838. * The pmbus_sensor_attr structure describes one sensor attribute. This
  839. * description includes a reference to the associated limit attributes.
  840. */
  841. struct pmbus_sensor_attr {
  842. u16 reg; /* sensor register */
  843. u16 gbit; /* generic status bit */
  844. u8 nlimit; /* # of limit registers */
  845. enum pmbus_sensor_classes class;/* sensor class */
  846. const char *label; /* sensor label */
  847. bool paged; /* true if paged sensor */
  848. bool update; /* true if update needed */
  849. bool compare; /* true if compare function needed */
  850. u32 func; /* sensor mask */
  851. u32 sfunc; /* sensor status mask */
  852. int sbase; /* status base register */
  853. const struct pmbus_limit_attr *limit;/* limit registers */
  854. };
  855. /*
  856. * Add a set of limit attributes and, if supported, the associated
  857. * alarm attributes.
  858. * returns 0 if no alarm register found, 1 if an alarm register was found,
  859. * < 0 on errors.
  860. */
  861. static int pmbus_add_limit_attrs(struct i2c_client *client,
  862. struct pmbus_data *data,
  863. const struct pmbus_driver_info *info,
  864. const char *name, int index, int page,
  865. struct pmbus_sensor *base,
  866. const struct pmbus_sensor_attr *attr)
  867. {
  868. const struct pmbus_limit_attr *l = attr->limit;
  869. int nlimit = attr->nlimit;
  870. int have_alarm = 0;
  871. int i, ret;
  872. struct pmbus_sensor *curr;
  873. for (i = 0; i < nlimit; i++) {
  874. if (pmbus_check_word_register(client, page, l->reg)) {
  875. curr = pmbus_add_sensor(data, name, l->attr, index,
  876. page, l->reg, attr->class,
  877. attr->update || l->update,
  878. false);
  879. if (!curr)
  880. return -ENOMEM;
  881. if (l->sbit && (info->func[page] & attr->sfunc)) {
  882. ret = pmbus_add_boolean(data, name,
  883. l->alarm, index,
  884. attr->compare ? l->low ? curr : base
  885. : NULL,
  886. attr->compare ? l->low ? base : curr
  887. : NULL,
  888. attr->sbase + page, l->sbit);
  889. if (ret)
  890. return ret;
  891. have_alarm = 1;
  892. }
  893. }
  894. l++;
  895. }
  896. return have_alarm;
  897. }
  898. static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
  899. struct pmbus_data *data,
  900. const struct pmbus_driver_info *info,
  901. const char *name,
  902. int index, int page,
  903. const struct pmbus_sensor_attr *attr,
  904. bool paged)
  905. {
  906. struct pmbus_sensor *base;
  907. bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */
  908. int ret;
  909. if (attr->label) {
  910. ret = pmbus_add_label(data, name, index, attr->label,
  911. paged ? page + 1 : 0);
  912. if (ret)
  913. return ret;
  914. }
  915. base = pmbus_add_sensor(data, name, "input", index, page, attr->reg,
  916. attr->class, true, true);
  917. if (!base)
  918. return -ENOMEM;
  919. if (attr->sfunc) {
  920. ret = pmbus_add_limit_attrs(client, data, info, name,
  921. index, page, base, attr);
  922. if (ret < 0)
  923. return ret;
  924. /*
  925. * Add generic alarm attribute only if there are no individual
  926. * alarm attributes, if there is a global alarm bit, and if
  927. * the generic status register (word or byte, depending on
  928. * which global bit is set) for this page is accessible.
  929. */
  930. if (!ret && attr->gbit &&
  931. (!upper || (upper && data->has_status_word)) &&
  932. pmbus_check_status_register(client, page)) {
  933. ret = pmbus_add_boolean(data, name, "alarm", index,
  934. NULL, NULL,
  935. PB_STATUS_BASE + page,
  936. attr->gbit);
  937. if (ret)
  938. return ret;
  939. }
  940. }
  941. return 0;
  942. }
  943. static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info,
  944. const struct pmbus_sensor_attr *attr)
  945. {
  946. int p;
  947. if (attr->paged)
  948. return true;
  949. /*
  950. * Some attributes may be present on more than one page despite
  951. * not being marked with the paged attribute. If that is the case,
  952. * then treat the sensor as being paged and add the page suffix to the
  953. * attribute name.
  954. * We don't just add the paged attribute to all such attributes, in
  955. * order to maintain the un-suffixed labels in the case where the
  956. * attribute is only on page 0.
  957. */
  958. for (p = 1; p < info->pages; p++) {
  959. if (info->func[p] & attr->func)
  960. return true;
  961. }
  962. return false;
  963. }
  964. static int pmbus_add_sensor_attrs(struct i2c_client *client,
  965. struct pmbus_data *data,
  966. const char *name,
  967. const struct pmbus_sensor_attr *attrs,
  968. int nattrs)
  969. {
  970. const struct pmbus_driver_info *info = data->info;
  971. int index, i;
  972. int ret;
  973. index = 1;
  974. for (i = 0; i < nattrs; i++) {
  975. int page, pages;
  976. bool paged = pmbus_sensor_is_paged(info, attrs);
  977. pages = paged ? info->pages : 1;
  978. for (page = 0; page < pages; page++) {
  979. if (!(info->func[page] & attrs->func))
  980. continue;
  981. ret = pmbus_add_sensor_attrs_one(client, data, info,
  982. name, index, page,
  983. attrs, paged);
  984. if (ret)
  985. return ret;
  986. index++;
  987. }
  988. attrs++;
  989. }
  990. return 0;
  991. }
  992. static const struct pmbus_limit_attr vin_limit_attrs[] = {
  993. {
  994. .reg = PMBUS_VIN_UV_WARN_LIMIT,
  995. .attr = "min",
  996. .alarm = "min_alarm",
  997. .sbit = PB_VOLTAGE_UV_WARNING,
  998. }, {
  999. .reg = PMBUS_VIN_UV_FAULT_LIMIT,
  1000. .attr = "lcrit",
  1001. .alarm = "lcrit_alarm",
  1002. .sbit = PB_VOLTAGE_UV_FAULT,
  1003. }, {
  1004. .reg = PMBUS_VIN_OV_WARN_LIMIT,
  1005. .attr = "max",
  1006. .alarm = "max_alarm",
  1007. .sbit = PB_VOLTAGE_OV_WARNING,
  1008. }, {
  1009. .reg = PMBUS_VIN_OV_FAULT_LIMIT,
  1010. .attr = "crit",
  1011. .alarm = "crit_alarm",
  1012. .sbit = PB_VOLTAGE_OV_FAULT,
  1013. }, {
  1014. .reg = PMBUS_VIRT_READ_VIN_AVG,
  1015. .update = true,
  1016. .attr = "average",
  1017. }, {
  1018. .reg = PMBUS_VIRT_READ_VIN_MIN,
  1019. .update = true,
  1020. .attr = "lowest",
  1021. }, {
  1022. .reg = PMBUS_VIRT_READ_VIN_MAX,
  1023. .update = true,
  1024. .attr = "highest",
  1025. }, {
  1026. .reg = PMBUS_VIRT_RESET_VIN_HISTORY,
  1027. .attr = "reset_history",
  1028. },
  1029. };
  1030. static const struct pmbus_limit_attr vmon_limit_attrs[] = {
  1031. {
  1032. .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
  1033. .attr = "min",
  1034. .alarm = "min_alarm",
  1035. .sbit = PB_VOLTAGE_UV_WARNING,
  1036. }, {
  1037. .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
  1038. .attr = "lcrit",
  1039. .alarm = "lcrit_alarm",
  1040. .sbit = PB_VOLTAGE_UV_FAULT,
  1041. }, {
  1042. .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
  1043. .attr = "max",
  1044. .alarm = "max_alarm",
  1045. .sbit = PB_VOLTAGE_OV_WARNING,
  1046. }, {
  1047. .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
  1048. .attr = "crit",
  1049. .alarm = "crit_alarm",
  1050. .sbit = PB_VOLTAGE_OV_FAULT,
  1051. }
  1052. };
  1053. static const struct pmbus_limit_attr vout_limit_attrs[] = {
  1054. {
  1055. .reg = PMBUS_VOUT_UV_WARN_LIMIT,
  1056. .attr = "min",
  1057. .alarm = "min_alarm",
  1058. .sbit = PB_VOLTAGE_UV_WARNING,
  1059. }, {
  1060. .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
  1061. .attr = "lcrit",
  1062. .alarm = "lcrit_alarm",
  1063. .sbit = PB_VOLTAGE_UV_FAULT,
  1064. }, {
  1065. .reg = PMBUS_VOUT_OV_WARN_LIMIT,
  1066. .attr = "max",
  1067. .alarm = "max_alarm",
  1068. .sbit = PB_VOLTAGE_OV_WARNING,
  1069. }, {
  1070. .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
  1071. .attr = "crit",
  1072. .alarm = "crit_alarm",
  1073. .sbit = PB_VOLTAGE_OV_FAULT,
  1074. }, {
  1075. .reg = PMBUS_VIRT_READ_VOUT_AVG,
  1076. .update = true,
  1077. .attr = "average",
  1078. }, {
  1079. .reg = PMBUS_VIRT_READ_VOUT_MIN,
  1080. .update = true,
  1081. .attr = "lowest",
  1082. }, {
  1083. .reg = PMBUS_VIRT_READ_VOUT_MAX,
  1084. .update = true,
  1085. .attr = "highest",
  1086. }, {
  1087. .reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
  1088. .attr = "reset_history",
  1089. }
  1090. };
  1091. static const struct pmbus_sensor_attr voltage_attributes[] = {
  1092. {
  1093. .reg = PMBUS_READ_VIN,
  1094. .class = PSC_VOLTAGE_IN,
  1095. .label = "vin",
  1096. .func = PMBUS_HAVE_VIN,
  1097. .sfunc = PMBUS_HAVE_STATUS_INPUT,
  1098. .sbase = PB_STATUS_INPUT_BASE,
  1099. .gbit = PB_STATUS_VIN_UV,
  1100. .limit = vin_limit_attrs,
  1101. .nlimit = ARRAY_SIZE(vin_limit_attrs),
  1102. }, {
  1103. .reg = PMBUS_VIRT_READ_VMON,
  1104. .class = PSC_VOLTAGE_IN,
  1105. .label = "vmon",
  1106. .func = PMBUS_HAVE_VMON,
  1107. .sfunc = PMBUS_HAVE_STATUS_VMON,
  1108. .sbase = PB_STATUS_VMON_BASE,
  1109. .limit = vmon_limit_attrs,
  1110. .nlimit = ARRAY_SIZE(vmon_limit_attrs),
  1111. }, {
  1112. .reg = PMBUS_READ_VCAP,
  1113. .class = PSC_VOLTAGE_IN,
  1114. .label = "vcap",
  1115. .func = PMBUS_HAVE_VCAP,
  1116. }, {
  1117. .reg = PMBUS_READ_VOUT,
  1118. .class = PSC_VOLTAGE_OUT,
  1119. .label = "vout",
  1120. .paged = true,
  1121. .func = PMBUS_HAVE_VOUT,
  1122. .sfunc = PMBUS_HAVE_STATUS_VOUT,
  1123. .sbase = PB_STATUS_VOUT_BASE,
  1124. .gbit = PB_STATUS_VOUT_OV,
  1125. .limit = vout_limit_attrs,
  1126. .nlimit = ARRAY_SIZE(vout_limit_attrs),
  1127. }
  1128. };
  1129. /* Current attributes */
  1130. static const struct pmbus_limit_attr iin_limit_attrs[] = {
  1131. {
  1132. .reg = PMBUS_IIN_OC_WARN_LIMIT,
  1133. .attr = "max",
  1134. .alarm = "max_alarm",
  1135. .sbit = PB_IIN_OC_WARNING,
  1136. }, {
  1137. .reg = PMBUS_IIN_OC_FAULT_LIMIT,
  1138. .attr = "crit",
  1139. .alarm = "crit_alarm",
  1140. .sbit = PB_IIN_OC_FAULT,
  1141. }, {
  1142. .reg = PMBUS_VIRT_READ_IIN_AVG,
  1143. .update = true,
  1144. .attr = "average",
  1145. }, {
  1146. .reg = PMBUS_VIRT_READ_IIN_MIN,
  1147. .update = true,
  1148. .attr = "lowest",
  1149. }, {
  1150. .reg = PMBUS_VIRT_READ_IIN_MAX,
  1151. .update = true,
  1152. .attr = "highest",
  1153. }, {
  1154. .reg = PMBUS_VIRT_RESET_IIN_HISTORY,
  1155. .attr = "reset_history",
  1156. }
  1157. };
  1158. static const struct pmbus_limit_attr iout_limit_attrs[] = {
  1159. {
  1160. .reg = PMBUS_IOUT_OC_WARN_LIMIT,
  1161. .attr = "max",
  1162. .alarm = "max_alarm",
  1163. .sbit = PB_IOUT_OC_WARNING,
  1164. }, {
  1165. .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
  1166. .attr = "lcrit",
  1167. .alarm = "lcrit_alarm",
  1168. .sbit = PB_IOUT_UC_FAULT,
  1169. }, {
  1170. .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
  1171. .attr = "crit",
  1172. .alarm = "crit_alarm",
  1173. .sbit = PB_IOUT_OC_FAULT,
  1174. }, {
  1175. .reg = PMBUS_VIRT_READ_IOUT_AVG,
  1176. .update = true,
  1177. .attr = "average",
  1178. }, {
  1179. .reg = PMBUS_VIRT_READ_IOUT_MIN,
  1180. .update = true,
  1181. .attr = "lowest",
  1182. }, {
  1183. .reg = PMBUS_VIRT_READ_IOUT_MAX,
  1184. .update = true,
  1185. .attr = "highest",
  1186. }, {
  1187. .reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
  1188. .attr = "reset_history",
  1189. }
  1190. };
  1191. static const struct pmbus_sensor_attr current_attributes[] = {
  1192. {
  1193. .reg = PMBUS_READ_IIN,
  1194. .class = PSC_CURRENT_IN,
  1195. .label = "iin",
  1196. .func = PMBUS_HAVE_IIN,
  1197. .sfunc = PMBUS_HAVE_STATUS_INPUT,
  1198. .sbase = PB_STATUS_INPUT_BASE,
  1199. .gbit = PB_STATUS_INPUT,
  1200. .limit = iin_limit_attrs,
  1201. .nlimit = ARRAY_SIZE(iin_limit_attrs),
  1202. }, {
  1203. .reg = PMBUS_READ_IOUT,
  1204. .class = PSC_CURRENT_OUT,
  1205. .label = "iout",
  1206. .paged = true,
  1207. .func = PMBUS_HAVE_IOUT,
  1208. .sfunc = PMBUS_HAVE_STATUS_IOUT,
  1209. .sbase = PB_STATUS_IOUT_BASE,
  1210. .gbit = PB_STATUS_IOUT_OC,
  1211. .limit = iout_limit_attrs,
  1212. .nlimit = ARRAY_SIZE(iout_limit_attrs),
  1213. }
  1214. };
  1215. /* Power attributes */
  1216. static const struct pmbus_limit_attr pin_limit_attrs[] = {
  1217. {
  1218. .reg = PMBUS_PIN_OP_WARN_LIMIT,
  1219. .attr = "max",
  1220. .alarm = "alarm",
  1221. .sbit = PB_PIN_OP_WARNING,
  1222. }, {
  1223. .reg = PMBUS_VIRT_READ_PIN_AVG,
  1224. .update = true,
  1225. .attr = "average",
  1226. }, {
  1227. .reg = PMBUS_VIRT_READ_PIN_MIN,
  1228. .update = true,
  1229. .attr = "input_lowest",
  1230. }, {
  1231. .reg = PMBUS_VIRT_READ_PIN_MAX,
  1232. .update = true,
  1233. .attr = "input_highest",
  1234. }, {
  1235. .reg = PMBUS_VIRT_RESET_PIN_HISTORY,
  1236. .attr = "reset_history",
  1237. }
  1238. };
  1239. static const struct pmbus_limit_attr pout_limit_attrs[] = {
  1240. {
  1241. .reg = PMBUS_POUT_MAX,
  1242. .attr = "cap",
  1243. .alarm = "cap_alarm",
  1244. .sbit = PB_POWER_LIMITING,
  1245. }, {
  1246. .reg = PMBUS_POUT_OP_WARN_LIMIT,
  1247. .attr = "max",
  1248. .alarm = "max_alarm",
  1249. .sbit = PB_POUT_OP_WARNING,
  1250. }, {
  1251. .reg = PMBUS_POUT_OP_FAULT_LIMIT,
  1252. .attr = "crit",
  1253. .alarm = "crit_alarm",
  1254. .sbit = PB_POUT_OP_FAULT,
  1255. }, {
  1256. .reg = PMBUS_VIRT_READ_POUT_AVG,
  1257. .update = true,
  1258. .attr = "average",
  1259. }, {
  1260. .reg = PMBUS_VIRT_READ_POUT_MIN,
  1261. .update = true,
  1262. .attr = "input_lowest",
  1263. }, {
  1264. .reg = PMBUS_VIRT_READ_POUT_MAX,
  1265. .update = true,
  1266. .attr = "input_highest",
  1267. }, {
  1268. .reg = PMBUS_VIRT_RESET_POUT_HISTORY,
  1269. .attr = "reset_history",
  1270. }
  1271. };
  1272. static const struct pmbus_sensor_attr power_attributes[] = {
  1273. {
  1274. .reg = PMBUS_READ_PIN,
  1275. .class = PSC_POWER,
  1276. .label = "pin",
  1277. .func = PMBUS_HAVE_PIN,
  1278. .sfunc = PMBUS_HAVE_STATUS_INPUT,
  1279. .sbase = PB_STATUS_INPUT_BASE,
  1280. .gbit = PB_STATUS_INPUT,
  1281. .limit = pin_limit_attrs,
  1282. .nlimit = ARRAY_SIZE(pin_limit_attrs),
  1283. }, {
  1284. .reg = PMBUS_READ_POUT,
  1285. .class = PSC_POWER,
  1286. .label = "pout",
  1287. .paged = true,
  1288. .func = PMBUS_HAVE_POUT,
  1289. .sfunc = PMBUS_HAVE_STATUS_IOUT,
  1290. .sbase = PB_STATUS_IOUT_BASE,
  1291. .limit = pout_limit_attrs,
  1292. .nlimit = ARRAY_SIZE(pout_limit_attrs),
  1293. }
  1294. };
  1295. /* Temperature atributes */
  1296. static const struct pmbus_limit_attr temp_limit_attrs[] = {
  1297. {
  1298. .reg = PMBUS_UT_WARN_LIMIT,
  1299. .low = true,
  1300. .attr = "min",
  1301. .alarm = "min_alarm",
  1302. .sbit = PB_TEMP_UT_WARNING,
  1303. }, {
  1304. .reg = PMBUS_UT_FAULT_LIMIT,
  1305. .low = true,
  1306. .attr = "lcrit",
  1307. .alarm = "lcrit_alarm",
  1308. .sbit = PB_TEMP_UT_FAULT,
  1309. }, {
  1310. .reg = PMBUS_OT_WARN_LIMIT,
  1311. .attr = "max",
  1312. .alarm = "max_alarm",
  1313. .sbit = PB_TEMP_OT_WARNING,
  1314. }, {
  1315. .reg = PMBUS_OT_FAULT_LIMIT,
  1316. .attr = "crit",
  1317. .alarm = "crit_alarm",
  1318. .sbit = PB_TEMP_OT_FAULT,
  1319. }, {
  1320. .reg = PMBUS_VIRT_READ_TEMP_MIN,
  1321. .attr = "lowest",
  1322. }, {
  1323. .reg = PMBUS_VIRT_READ_TEMP_AVG,
  1324. .attr = "average",
  1325. }, {
  1326. .reg = PMBUS_VIRT_READ_TEMP_MAX,
  1327. .attr = "highest",
  1328. }, {
  1329. .reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
  1330. .attr = "reset_history",
  1331. }
  1332. };
  1333. static const struct pmbus_limit_attr temp_limit_attrs2[] = {
  1334. {
  1335. .reg = PMBUS_UT_WARN_LIMIT,
  1336. .low = true,
  1337. .attr = "min",
  1338. .alarm = "min_alarm",
  1339. .sbit = PB_TEMP_UT_WARNING,
  1340. }, {
  1341. .reg = PMBUS_UT_FAULT_LIMIT,
  1342. .low = true,
  1343. .attr = "lcrit",
  1344. .alarm = "lcrit_alarm",
  1345. .sbit = PB_TEMP_UT_FAULT,
  1346. }, {
  1347. .reg = PMBUS_OT_WARN_LIMIT,
  1348. .attr = "max",
  1349. .alarm = "max_alarm",
  1350. .sbit = PB_TEMP_OT_WARNING,
  1351. }, {
  1352. .reg = PMBUS_OT_FAULT_LIMIT,
  1353. .attr = "crit",
  1354. .alarm = "crit_alarm",
  1355. .sbit = PB_TEMP_OT_FAULT,
  1356. }, {
  1357. .reg = PMBUS_VIRT_READ_TEMP2_MIN,
  1358. .attr = "lowest",
  1359. }, {
  1360. .reg = PMBUS_VIRT_READ_TEMP2_AVG,
  1361. .attr = "average",
  1362. }, {
  1363. .reg = PMBUS_VIRT_READ_TEMP2_MAX,
  1364. .attr = "highest",
  1365. }, {
  1366. .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
  1367. .attr = "reset_history",
  1368. }
  1369. };
  1370. static const struct pmbus_limit_attr temp_limit_attrs3[] = {
  1371. {
  1372. .reg = PMBUS_UT_WARN_LIMIT,
  1373. .low = true,
  1374. .attr = "min",
  1375. .alarm = "min_alarm",
  1376. .sbit = PB_TEMP_UT_WARNING,
  1377. }, {
  1378. .reg = PMBUS_UT_FAULT_LIMIT,
  1379. .low = true,
  1380. .attr = "lcrit",
  1381. .alarm = "lcrit_alarm",
  1382. .sbit = PB_TEMP_UT_FAULT,
  1383. }, {
  1384. .reg = PMBUS_OT_WARN_LIMIT,
  1385. .attr = "max",
  1386. .alarm = "max_alarm",
  1387. .sbit = PB_TEMP_OT_WARNING,
  1388. }, {
  1389. .reg = PMBUS_OT_FAULT_LIMIT,
  1390. .attr = "crit",
  1391. .alarm = "crit_alarm",
  1392. .sbit = PB_TEMP_OT_FAULT,
  1393. }
  1394. };
  1395. static const struct pmbus_sensor_attr temp_attributes[] = {
  1396. {
  1397. .reg = PMBUS_READ_TEMPERATURE_1,
  1398. .class = PSC_TEMPERATURE,
  1399. .paged = true,
  1400. .update = true,
  1401. .compare = true,
  1402. .func = PMBUS_HAVE_TEMP,
  1403. .sfunc = PMBUS_HAVE_STATUS_TEMP,
  1404. .sbase = PB_STATUS_TEMP_BASE,
  1405. .gbit = PB_STATUS_TEMPERATURE,
  1406. .limit = temp_limit_attrs,
  1407. .nlimit = ARRAY_SIZE(temp_limit_attrs),
  1408. }, {
  1409. .reg = PMBUS_READ_TEMPERATURE_2,
  1410. .class = PSC_TEMPERATURE,
  1411. .paged = true,
  1412. .update = true,
  1413. .compare = true,
  1414. .func = PMBUS_HAVE_TEMP2,
  1415. .sfunc = PMBUS_HAVE_STATUS_TEMP,
  1416. .sbase = PB_STATUS_TEMP_BASE,
  1417. .gbit = PB_STATUS_TEMPERATURE,
  1418. .limit = temp_limit_attrs2,
  1419. .nlimit = ARRAY_SIZE(temp_limit_attrs2),
  1420. }, {
  1421. .reg = PMBUS_READ_TEMPERATURE_3,
  1422. .class = PSC_TEMPERATURE,
  1423. .paged = true,
  1424. .update = true,
  1425. .compare = true,
  1426. .func = PMBUS_HAVE_TEMP3,
  1427. .sfunc = PMBUS_HAVE_STATUS_TEMP,
  1428. .sbase = PB_STATUS_TEMP_BASE,
  1429. .gbit = PB_STATUS_TEMPERATURE,
  1430. .limit = temp_limit_attrs3,
  1431. .nlimit = ARRAY_SIZE(temp_limit_attrs3),
  1432. }
  1433. };
  1434. static const int pmbus_fan_registers[] = {
  1435. PMBUS_READ_FAN_SPEED_1,
  1436. PMBUS_READ_FAN_SPEED_2,
  1437. PMBUS_READ_FAN_SPEED_3,
  1438. PMBUS_READ_FAN_SPEED_4
  1439. };
  1440. static const int pmbus_fan_config_registers[] = {
  1441. PMBUS_FAN_CONFIG_12,
  1442. PMBUS_FAN_CONFIG_12,
  1443. PMBUS_FAN_CONFIG_34,
  1444. PMBUS_FAN_CONFIG_34
  1445. };
  1446. static const int pmbus_fan_status_registers[] = {
  1447. PMBUS_STATUS_FAN_12,
  1448. PMBUS_STATUS_FAN_12,
  1449. PMBUS_STATUS_FAN_34,
  1450. PMBUS_STATUS_FAN_34
  1451. };
  1452. static const u32 pmbus_fan_flags[] = {
  1453. PMBUS_HAVE_FAN12,
  1454. PMBUS_HAVE_FAN12,
  1455. PMBUS_HAVE_FAN34,
  1456. PMBUS_HAVE_FAN34
  1457. };
  1458. static const u32 pmbus_fan_status_flags[] = {
  1459. PMBUS_HAVE_STATUS_FAN12,
  1460. PMBUS_HAVE_STATUS_FAN12,
  1461. PMBUS_HAVE_STATUS_FAN34,
  1462. PMBUS_HAVE_STATUS_FAN34
  1463. };
  1464. /* Fans */
  1465. static int pmbus_add_fan_attributes(struct i2c_client *client,
  1466. struct pmbus_data *data)
  1467. {
  1468. const struct pmbus_driver_info *info = data->info;
  1469. int index = 1;
  1470. int page;
  1471. int ret;
  1472. for (page = 0; page < info->pages; page++) {
  1473. int f;
  1474. for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
  1475. int regval;
  1476. if (!(info->func[page] & pmbus_fan_flags[f]))
  1477. break;
  1478. if (!pmbus_check_word_register(client, page,
  1479. pmbus_fan_registers[f]))
  1480. break;
  1481. /*
  1482. * Skip fan if not installed.
  1483. * Each fan configuration register covers multiple fans,
  1484. * so we have to do some magic.
  1485. */
  1486. regval = _pmbus_read_byte_data(client, page,
  1487. pmbus_fan_config_registers[f]);
  1488. if (regval < 0 ||
  1489. (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
  1490. continue;
  1491. if (pmbus_add_sensor(data, "fan", "input", index,
  1492. page, pmbus_fan_registers[f],
  1493. PSC_FAN, true, true) == NULL)
  1494. return -ENOMEM;
  1495. /*
  1496. * Each fan status register covers multiple fans,
  1497. * so we have to do some magic.
  1498. */
  1499. if ((info->func[page] & pmbus_fan_status_flags[f]) &&
  1500. pmbus_check_byte_register(client,
  1501. page, pmbus_fan_status_registers[f])) {
  1502. int base;
  1503. if (f > 1) /* fan 3, 4 */
  1504. base = PB_STATUS_FAN34_BASE + page;
  1505. else
  1506. base = PB_STATUS_FAN_BASE + page;
  1507. ret = pmbus_add_boolean(data, "fan",
  1508. "alarm", index, NULL, NULL, base,
  1509. PB_FAN_FAN1_WARNING >> (f & 1));
  1510. if (ret)
  1511. return ret;
  1512. ret = pmbus_add_boolean(data, "fan",
  1513. "fault", index, NULL, NULL, base,
  1514. PB_FAN_FAN1_FAULT >> (f & 1));
  1515. if (ret)
  1516. return ret;
  1517. }
  1518. index++;
  1519. }
  1520. }
  1521. return 0;
  1522. }
  1523. static int pmbus_find_attributes(struct i2c_client *client,
  1524. struct pmbus_data *data)
  1525. {
  1526. int ret;
  1527. /* Voltage sensors */
  1528. ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
  1529. ARRAY_SIZE(voltage_attributes));
  1530. if (ret)
  1531. return ret;
  1532. /* Current sensors */
  1533. ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
  1534. ARRAY_SIZE(current_attributes));
  1535. if (ret)
  1536. return ret;
  1537. /* Power sensors */
  1538. ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
  1539. ARRAY_SIZE(power_attributes));
  1540. if (ret)
  1541. return ret;
  1542. /* Temperature sensors */
  1543. ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
  1544. ARRAY_SIZE(temp_attributes));
  1545. if (ret)
  1546. return ret;
  1547. /* Fans */
  1548. ret = pmbus_add_fan_attributes(client, data);
  1549. return ret;
  1550. }
  1551. /*
  1552. * Identify chip parameters.
  1553. * This function is called for all chips.
  1554. */
  1555. static int pmbus_identify_common(struct i2c_client *client,
  1556. struct pmbus_data *data, int page)
  1557. {
  1558. int vout_mode = -1;
  1559. if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
  1560. vout_mode = _pmbus_read_byte_data(client, page,
  1561. PMBUS_VOUT_MODE);
  1562. if (vout_mode >= 0 && vout_mode != 0xff) {
  1563. /*
  1564. * Not all chips support the VOUT_MODE command,
  1565. * so a failure to read it is not an error.
  1566. */
  1567. switch (vout_mode >> 5) {
  1568. case 0: /* linear mode */
  1569. if (data->info->format[PSC_VOLTAGE_OUT] != linear)
  1570. return -ENODEV;
  1571. data->exponent[page] = ((s8)(vout_mode << 3)) >> 3;
  1572. break;
  1573. case 1: /* VID mode */
  1574. if (data->info->format[PSC_VOLTAGE_OUT] != vid)
  1575. return -ENODEV;
  1576. break;
  1577. case 2: /* direct mode */
  1578. if (data->info->format[PSC_VOLTAGE_OUT] != direct)
  1579. return -ENODEV;
  1580. break;
  1581. default:
  1582. return -ENODEV;
  1583. }
  1584. }
  1585. pmbus_clear_fault_page(client, page);
  1586. return 0;
  1587. }
  1588. static int pmbus_read_status_byte(struct i2c_client *client, int page)
  1589. {
  1590. return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
  1591. }
  1592. static int pmbus_read_status_word(struct i2c_client *client, int page)
  1593. {
  1594. return _pmbus_read_word_data(client, page, PMBUS_STATUS_WORD);
  1595. }
  1596. static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
  1597. struct pmbus_driver_info *info)
  1598. {
  1599. struct device *dev = &client->dev;
  1600. int page, ret;
  1601. /*
  1602. * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
  1603. * to use PMBUS_STATUS_BYTE instead if that is the case.
  1604. * Bail out if both registers are not supported.
  1605. */
  1606. data->read_status = pmbus_read_status_word;
  1607. ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
  1608. if (ret < 0 || ret == 0xffff) {
  1609. data->read_status = pmbus_read_status_byte;
  1610. ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
  1611. if (ret < 0 || ret == 0xff) {
  1612. dev_err(dev, "PMBus status register not found\n");
  1613. return -ENODEV;
  1614. }
  1615. } else {
  1616. data->has_status_word = true;
  1617. }
  1618. /* Enable PEC if the controller supports it */
  1619. ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
  1620. if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
  1621. client->flags |= I2C_CLIENT_PEC;
  1622. if (data->info->pages)
  1623. pmbus_clear_faults(client);
  1624. else
  1625. pmbus_clear_fault_page(client, -1);
  1626. if (info->identify) {
  1627. ret = (*info->identify)(client, info);
  1628. if (ret < 0) {
  1629. dev_err(dev, "Chip identification failed\n");
  1630. return ret;
  1631. }
  1632. }
  1633. if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
  1634. dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
  1635. return -ENODEV;
  1636. }
  1637. for (page = 0; page < info->pages; page++) {
  1638. ret = pmbus_identify_common(client, data, page);
  1639. if (ret < 0) {
  1640. dev_err(dev, "Failed to identify chip capabilities\n");
  1641. return ret;
  1642. }
  1643. }
  1644. return 0;
  1645. }
  1646. #if IS_ENABLED(CONFIG_REGULATOR)
  1647. static int pmbus_regulator_is_enabled(struct regulator_dev *rdev)
  1648. {
  1649. struct device *dev = rdev_get_dev(rdev);
  1650. struct i2c_client *client = to_i2c_client(dev->parent);
  1651. u8 page = rdev_get_id(rdev);
  1652. int ret;
  1653. ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION);
  1654. if (ret < 0)
  1655. return ret;
  1656. return !!(ret & PB_OPERATION_CONTROL_ON);
  1657. }
  1658. static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable)
  1659. {
  1660. struct device *dev = rdev_get_dev(rdev);
  1661. struct i2c_client *client = to_i2c_client(dev->parent);
  1662. u8 page = rdev_get_id(rdev);
  1663. return pmbus_update_byte_data(client, page, PMBUS_OPERATION,
  1664. PB_OPERATION_CONTROL_ON,
  1665. enable ? PB_OPERATION_CONTROL_ON : 0);
  1666. }
  1667. static int pmbus_regulator_enable(struct regulator_dev *rdev)
  1668. {
  1669. return _pmbus_regulator_on_off(rdev, 1);
  1670. }
  1671. static int pmbus_regulator_disable(struct regulator_dev *rdev)
  1672. {
  1673. return _pmbus_regulator_on_off(rdev, 0);
  1674. }
  1675. const struct regulator_ops pmbus_regulator_ops = {
  1676. .enable = pmbus_regulator_enable,
  1677. .disable = pmbus_regulator_disable,
  1678. .is_enabled = pmbus_regulator_is_enabled,
  1679. };
  1680. EXPORT_SYMBOL_GPL(pmbus_regulator_ops);
  1681. static int pmbus_regulator_register(struct pmbus_data *data)
  1682. {
  1683. struct device *dev = data->dev;
  1684. const struct pmbus_driver_info *info = data->info;
  1685. const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
  1686. struct regulator_dev *rdev;
  1687. int i;
  1688. for (i = 0; i < info->num_regulators; i++) {
  1689. struct regulator_config config = { };
  1690. config.dev = dev;
  1691. config.driver_data = data;
  1692. if (pdata && pdata->reg_init_data)
  1693. config.init_data = &pdata->reg_init_data[i];
  1694. rdev = devm_regulator_register(dev, &info->reg_desc[i],
  1695. &config);
  1696. if (IS_ERR(rdev)) {
  1697. dev_err(dev, "Failed to register %s regulator\n",
  1698. info->reg_desc[i].name);
  1699. return PTR_ERR(rdev);
  1700. }
  1701. }
  1702. return 0;
  1703. }
  1704. #else
  1705. static int pmbus_regulator_register(struct pmbus_data *data)
  1706. {
  1707. return 0;
  1708. }
  1709. #endif
  1710. static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */
  1711. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1712. static int pmbus_debugfs_get(void *data, u64 *val)
  1713. {
  1714. int rc;
  1715. struct pmbus_debugfs_entry *entry = data;
  1716. rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
  1717. if (rc < 0)
  1718. return rc;
  1719. *val = rc;
  1720. return 0;
  1721. }
  1722. DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
  1723. "0x%02llx\n");
  1724. static int pmbus_debugfs_get_status(void *data, u64 *val)
  1725. {
  1726. int rc;
  1727. struct pmbus_debugfs_entry *entry = data;
  1728. struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
  1729. rc = pdata->read_status(entry->client, entry->page);
  1730. if (rc < 0)
  1731. return rc;
  1732. *val = rc;
  1733. return 0;
  1734. }
  1735. DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
  1736. NULL, "0x%04llx\n");
  1737. static int pmbus_init_debugfs(struct i2c_client *client,
  1738. struct pmbus_data *data)
  1739. {
  1740. int i, idx = 0;
  1741. char name[PMBUS_NAME_SIZE];
  1742. struct pmbus_debugfs_entry *entries;
  1743. if (!pmbus_debugfs_dir)
  1744. return -ENODEV;
  1745. /*
  1746. * Create the debugfs directory for this device. Use the hwmon device
  1747. * name to avoid conflicts (hwmon numbers are globally unique).
  1748. */
  1749. data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
  1750. pmbus_debugfs_dir);
  1751. if (IS_ERR_OR_NULL(data->debugfs)) {
  1752. data->debugfs = NULL;
  1753. return -ENODEV;
  1754. }
  1755. /* Allocate the max possible entries we need. */
  1756. entries = devm_kzalloc(data->dev,
  1757. sizeof(*entries) * (data->info->pages * 10),
  1758. GFP_KERNEL);
  1759. if (!entries)
  1760. return -ENOMEM;
  1761. for (i = 0; i < data->info->pages; ++i) {
  1762. /* Check accessibility of status register if it's not page 0 */
  1763. if (!i || pmbus_check_status_register(client, i)) {
  1764. /* No need to set reg as we have special read op. */
  1765. entries[idx].client = client;
  1766. entries[idx].page = i;
  1767. scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
  1768. debugfs_create_file(name, 0444, data->debugfs,
  1769. &entries[idx++],
  1770. &pmbus_debugfs_ops_status);
  1771. }
  1772. if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
  1773. entries[idx].client = client;
  1774. entries[idx].page = i;
  1775. entries[idx].reg = PMBUS_STATUS_VOUT;
  1776. scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
  1777. debugfs_create_file(name, 0444, data->debugfs,
  1778. &entries[idx++],
  1779. &pmbus_debugfs_ops);
  1780. }
  1781. if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
  1782. entries[idx].client = client;
  1783. entries[idx].page = i;
  1784. entries[idx].reg = PMBUS_STATUS_IOUT;
  1785. scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
  1786. debugfs_create_file(name, 0444, data->debugfs,
  1787. &entries[idx++],
  1788. &pmbus_debugfs_ops);
  1789. }
  1790. if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
  1791. entries[idx].client = client;
  1792. entries[idx].page = i;
  1793. entries[idx].reg = PMBUS_STATUS_INPUT;
  1794. scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
  1795. debugfs_create_file(name, 0444, data->debugfs,
  1796. &entries[idx++],
  1797. &pmbus_debugfs_ops);
  1798. }
  1799. if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
  1800. entries[idx].client = client;
  1801. entries[idx].page = i;
  1802. entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
  1803. scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
  1804. debugfs_create_file(name, 0444, data->debugfs,
  1805. &entries[idx++],
  1806. &pmbus_debugfs_ops);
  1807. }
  1808. if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
  1809. entries[idx].client = client;
  1810. entries[idx].page = i;
  1811. entries[idx].reg = PMBUS_STATUS_CML;
  1812. scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
  1813. debugfs_create_file(name, 0444, data->debugfs,
  1814. &entries[idx++],
  1815. &pmbus_debugfs_ops);
  1816. }
  1817. if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
  1818. entries[idx].client = client;
  1819. entries[idx].page = i;
  1820. entries[idx].reg = PMBUS_STATUS_OTHER;
  1821. scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
  1822. debugfs_create_file(name, 0444, data->debugfs,
  1823. &entries[idx++],
  1824. &pmbus_debugfs_ops);
  1825. }
  1826. if (pmbus_check_byte_register(client, i,
  1827. PMBUS_STATUS_MFR_SPECIFIC)) {
  1828. entries[idx].client = client;
  1829. entries[idx].page = i;
  1830. entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
  1831. scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
  1832. debugfs_create_file(name, 0444, data->debugfs,
  1833. &entries[idx++],
  1834. &pmbus_debugfs_ops);
  1835. }
  1836. if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
  1837. entries[idx].client = client;
  1838. entries[idx].page = i;
  1839. entries[idx].reg = PMBUS_STATUS_FAN_12;
  1840. scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
  1841. debugfs_create_file(name, 0444, data->debugfs,
  1842. &entries[idx++],
  1843. &pmbus_debugfs_ops);
  1844. }
  1845. if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
  1846. entries[idx].client = client;
  1847. entries[idx].page = i;
  1848. entries[idx].reg = PMBUS_STATUS_FAN_34;
  1849. scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
  1850. debugfs_create_file(name, 0444, data->debugfs,
  1851. &entries[idx++],
  1852. &pmbus_debugfs_ops);
  1853. }
  1854. }
  1855. return 0;
  1856. }
  1857. #else
  1858. static int pmbus_init_debugfs(struct i2c_client *client,
  1859. struct pmbus_data *data)
  1860. {
  1861. return 0;
  1862. }
  1863. #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
  1864. int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
  1865. struct pmbus_driver_info *info)
  1866. {
  1867. struct device *dev = &client->dev;
  1868. const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
  1869. struct pmbus_data *data;
  1870. int ret;
  1871. if (!info)
  1872. return -ENODEV;
  1873. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
  1874. | I2C_FUNC_SMBUS_BYTE_DATA
  1875. | I2C_FUNC_SMBUS_WORD_DATA))
  1876. return -ENODEV;
  1877. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  1878. if (!data)
  1879. return -ENOMEM;
  1880. i2c_set_clientdata(client, data);
  1881. mutex_init(&data->update_lock);
  1882. data->dev = dev;
  1883. if (pdata)
  1884. data->flags = pdata->flags;
  1885. data->info = info;
  1886. ret = pmbus_init_common(client, data, info);
  1887. if (ret < 0)
  1888. return ret;
  1889. ret = pmbus_find_attributes(client, data);
  1890. if (ret)
  1891. goto out_kfree;
  1892. /*
  1893. * If there are no attributes, something is wrong.
  1894. * Bail out instead of trying to register nothing.
  1895. */
  1896. if (!data->num_attributes) {
  1897. dev_err(dev, "No attributes found\n");
  1898. ret = -ENODEV;
  1899. goto out_kfree;
  1900. }
  1901. data->groups[0] = &data->group;
  1902. data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
  1903. data, data->groups);
  1904. if (IS_ERR(data->hwmon_dev)) {
  1905. ret = PTR_ERR(data->hwmon_dev);
  1906. dev_err(dev, "Failed to register hwmon device\n");
  1907. goto out_kfree;
  1908. }
  1909. ret = pmbus_regulator_register(data);
  1910. if (ret)
  1911. goto out_unregister;
  1912. ret = pmbus_init_debugfs(client, data);
  1913. if (ret)
  1914. dev_warn(dev, "Failed to register debugfs\n");
  1915. return 0;
  1916. out_unregister:
  1917. hwmon_device_unregister(data->hwmon_dev);
  1918. out_kfree:
  1919. kfree(data->group.attrs);
  1920. return ret;
  1921. }
  1922. EXPORT_SYMBOL_GPL(pmbus_do_probe);
  1923. int pmbus_do_remove(struct i2c_client *client)
  1924. {
  1925. struct pmbus_data *data = i2c_get_clientdata(client);
  1926. debugfs_remove_recursive(data->debugfs);
  1927. hwmon_device_unregister(data->hwmon_dev);
  1928. kfree(data->group.attrs);
  1929. return 0;
  1930. }
  1931. EXPORT_SYMBOL_GPL(pmbus_do_remove);
  1932. static int __init pmbus_core_init(void)
  1933. {
  1934. pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
  1935. if (IS_ERR(pmbus_debugfs_dir))
  1936. pmbus_debugfs_dir = NULL;
  1937. return 0;
  1938. }
  1939. static void __exit pmbus_core_exit(void)
  1940. {
  1941. debugfs_remove_recursive(pmbus_debugfs_dir);
  1942. }
  1943. module_init(pmbus_core_init);
  1944. module_exit(pmbus_core_exit);
  1945. MODULE_AUTHOR("Guenter Roeck");
  1946. MODULE_DESCRIPTION("PMBus core driver");
  1947. MODULE_LICENSE("GPL");