pmbus_core.c 59 KB

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