pmbus_core.c 46 KB

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