tps23881.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the TI TPS23881 PoE PSE Controller driver (I2C bus)
  4. *
  5. * Copyright (c) 2023 Bootlin, Kory Maincent <kory.maincent@bootlin.com>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/delay.h>
  9. #include <linux/firmware.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pse-pd/pse.h>
  16. #define TPS23881_MAX_CHANS 8
  17. #define TPS23881_MAX_IRQ_RETRIES 10
  18. #define TPS23881_REG_IT 0x0
  19. #define TPS23881_REG_IT_MASK 0x1
  20. #define TPS23881_REG_IT_DISF BIT(2)
  21. #define TPS23881_REG_IT_DETC BIT(3)
  22. #define TPS23881_REG_IT_CLASC BIT(4)
  23. #define TPS23881_REG_IT_IFAULT BIT(5)
  24. #define TPS23881_REG_IT_SUPF BIT(7)
  25. #define TPS23881_REG_DET_EVENT 0x5
  26. #define TPS23881_REG_FAULT 0x7
  27. #define TPS23881_REG_SUPF_EVENT 0xb
  28. #define TPS23881_REG_TSD BIT(7)
  29. #define TPS23881_REG_DISC 0xc
  30. #define TPS23881_REG_PW_STATUS 0x10
  31. #define TPS23881_REG_OP_MODE 0x12
  32. #define TPS23881_REG_DISC_EN 0x13
  33. #define TPS23881_OP_MODE_SEMIAUTO 0xaaaa
  34. #define TPS23881_REG_DIS_EN 0x13
  35. #define TPS23881_REG_DET_CLA_EN 0x14
  36. #define TPS23881_REG_GEN_MASK 0x17
  37. #define TPS23881_REG_CLCHE BIT(2)
  38. #define TPS23881_REG_DECHE BIT(3)
  39. #define TPS23881_REG_NBITACC BIT(5)
  40. #define TPS23881_REG_INTEN BIT(7)
  41. #define TPS23881_REG_PW_EN 0x19
  42. #define TPS23881_REG_RESET 0x1a
  43. #define TPS23881_REG_CLRAIN BIT(7)
  44. #define TPS23881_REG_2PAIR_POL1 0x1e
  45. #define TPS23881_REG_PORT_MAP 0x26
  46. #define TPS23881_REG_PORT_POWER 0x29
  47. #define TPS23881_REG_4PAIR_POL1 0x2a
  48. #define TPS23881_REG_INPUT_V 0x2e
  49. #define TPS23881_REG_CHAN1_A 0x30
  50. #define TPS23881_REG_CHAN1_V 0x32
  51. #define TPS23881_REG_FOLDBACK 0x40
  52. #define TPS23881_REG_TPON BIT(0)
  53. #define TPS23881_REG_FWREV 0x41
  54. #define TPS23881_REG_DEVID 0x43
  55. #define TPS23881_REG_CHAN1_CLASS 0x4c
  56. #define TPS23881_REG_SRAM_CTRL 0x60
  57. #define TPS23881_REG_SRAM_DATA 0x61
  58. #define TPS23881_UV_STEP 3662
  59. #define TPS23881_NA_STEP 89500
  60. #define TPS23881_MW_STEP 500
  61. #define TPS23881_MIN_PI_PW_LIMIT_MW 2000
  62. struct tps23881_port_desc {
  63. u8 chan[2];
  64. bool is_4p;
  65. int pw_pol;
  66. bool exist;
  67. };
  68. struct tps23881_priv {
  69. struct i2c_client *client;
  70. struct pse_controller_dev pcdev;
  71. struct device_node *np;
  72. struct tps23881_port_desc port[TPS23881_MAX_CHANS];
  73. };
  74. static struct tps23881_priv *to_tps23881_priv(struct pse_controller_dev *pcdev)
  75. {
  76. return container_of(pcdev, struct tps23881_priv, pcdev);
  77. }
  78. /*
  79. * Helper to extract a value from a u16 register value, which is made of two
  80. * u8 registers. The function calculates the bit offset based on the channel
  81. * and extracts the relevant bits using a provided field mask.
  82. *
  83. * @param reg_val: The u16 register value (composed of two u8 registers).
  84. * @param chan: The channel number (0-7).
  85. * @param field_offset: The base bit offset to apply (e.g., 0 or 4).
  86. * @param field_mask: The mask to apply to extract the required bits.
  87. * @return: The extracted value for the specific channel.
  88. */
  89. static u16 tps23881_calc_val(u16 reg_val, u8 chan, u8 field_offset,
  90. u16 field_mask)
  91. {
  92. if (chan >= 4)
  93. reg_val >>= 8;
  94. return (reg_val >> field_offset) & field_mask;
  95. }
  96. /*
  97. * Helper to combine individual channel values into a u16 register value.
  98. * The function sets the value for a specific channel in the appropriate
  99. * position.
  100. *
  101. * @param reg_val: The current u16 register value.
  102. * @param chan: The channel number (0-7).
  103. * @param field_offset: The base bit offset to apply (e.g., 0 or 4).
  104. * @param field_mask: The mask to apply for the field (e.g., 0x0F).
  105. * @param field_val: The value to set for the specific channel (masked by
  106. * field_mask).
  107. * @return: The updated u16 register value with the channel value set.
  108. */
  109. static u16 tps23881_set_val(u16 reg_val, u8 chan, u8 field_offset,
  110. u16 field_mask, u16 field_val)
  111. {
  112. field_val &= field_mask;
  113. if (chan < 4) {
  114. reg_val &= ~(field_mask << field_offset);
  115. reg_val |= (field_val << field_offset);
  116. } else {
  117. reg_val &= ~(field_mask << (field_offset + 8));
  118. reg_val |= (field_val << (field_offset + 8));
  119. }
  120. return reg_val;
  121. }
  122. static int
  123. tps23881_pi_set_pw_pol_limit(struct tps23881_priv *priv, int id, u8 pw_pol,
  124. bool is_4p)
  125. {
  126. struct i2c_client *client = priv->client;
  127. int ret, reg;
  128. u16 val;
  129. u8 chan;
  130. chan = priv->port[id].chan[0];
  131. if (!is_4p) {
  132. reg = TPS23881_REG_2PAIR_POL1 + (chan % 4);
  133. } else {
  134. /* One chan is enough to configure the 4p PI power limit */
  135. if ((chan % 4) < 2)
  136. reg = TPS23881_REG_4PAIR_POL1;
  137. else
  138. reg = TPS23881_REG_4PAIR_POL1 + 1;
  139. }
  140. ret = i2c_smbus_read_word_data(client, reg);
  141. if (ret < 0)
  142. return ret;
  143. val = tps23881_set_val(ret, chan, 0, 0xff, pw_pol);
  144. return i2c_smbus_write_word_data(client, reg, val);
  145. }
  146. static int tps23881_pi_enable_manual_pol(struct tps23881_priv *priv, int id)
  147. {
  148. struct i2c_client *client = priv->client;
  149. int ret;
  150. u8 chan;
  151. u16 val;
  152. ret = i2c_smbus_read_byte_data(client, TPS23881_REG_FOLDBACK);
  153. if (ret < 0)
  154. return ret;
  155. /* No need to test if the chan is PoE4 as setting either bit for a
  156. * 4P configured port disables the automatic configuration on both
  157. * channels.
  158. */
  159. chan = priv->port[id].chan[0];
  160. val = tps23881_set_val(ret, chan, 0, BIT(chan % 4), BIT(chan % 4));
  161. return i2c_smbus_write_byte_data(client, TPS23881_REG_FOLDBACK, val);
  162. }
  163. static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
  164. {
  165. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  166. struct i2c_client *client = priv->client;
  167. u8 chan;
  168. u16 val;
  169. int ret;
  170. if (id >= TPS23881_MAX_CHANS)
  171. return -ERANGE;
  172. chan = priv->port[id].chan[0];
  173. val = tps23881_set_val(0, chan, 0, BIT(chan % 4), BIT(chan % 4));
  174. if (priv->port[id].is_4p) {
  175. chan = priv->port[id].chan[1];
  176. val = tps23881_set_val(val, chan, 0, BIT(chan % 4),
  177. BIT(chan % 4));
  178. }
  179. ret = i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
  180. if (ret)
  181. return ret;
  182. /* Enable DC disconnect*/
  183. chan = priv->port[id].chan[0];
  184. ret = i2c_smbus_read_word_data(client, TPS23881_REG_DISC_EN);
  185. if (ret < 0)
  186. return ret;
  187. val = tps23881_set_val(ret, chan, 0, BIT(chan % 4), BIT(chan % 4));
  188. ret = i2c_smbus_write_word_data(client, TPS23881_REG_DISC_EN, val);
  189. if (ret)
  190. return ret;
  191. return 0;
  192. }
  193. static int tps23881_pi_disable(struct pse_controller_dev *pcdev, int id)
  194. {
  195. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  196. struct i2c_client *client = priv->client;
  197. u8 chan;
  198. u16 val;
  199. int ret;
  200. if (id >= TPS23881_MAX_CHANS)
  201. return -ERANGE;
  202. chan = priv->port[id].chan[0];
  203. val = tps23881_set_val(0, chan, 4, BIT(chan % 4), BIT(chan % 4));
  204. if (priv->port[id].is_4p) {
  205. chan = priv->port[id].chan[1];
  206. val = tps23881_set_val(val, chan, 4, BIT(chan % 4),
  207. BIT(chan % 4));
  208. }
  209. ret = i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
  210. if (ret)
  211. return ret;
  212. /* PWOFF command resets lots of register which need to be
  213. * configured again. According to the datasheet "It may take upwards
  214. * of 5ms after PWOFFn command for all register values to be updated"
  215. */
  216. mdelay(5);
  217. /* Disable DC disconnect*/
  218. chan = priv->port[id].chan[0];
  219. ret = i2c_smbus_read_word_data(client, TPS23881_REG_DISC_EN);
  220. if (ret < 0)
  221. return ret;
  222. val = tps23881_set_val(ret, chan, 0, 0, BIT(chan % 4));
  223. ret = i2c_smbus_write_word_data(client, TPS23881_REG_DISC_EN, val);
  224. if (ret)
  225. return ret;
  226. /* Enable detection and classification */
  227. ret = i2c_smbus_read_word_data(client, TPS23881_REG_DET_CLA_EN);
  228. if (ret < 0)
  229. return ret;
  230. chan = priv->port[id].chan[0];
  231. val = tps23881_set_val(ret, chan, 0, BIT(chan % 4), BIT(chan % 4));
  232. val = tps23881_set_val(val, chan, 4, BIT(chan % 4), BIT(chan % 4));
  233. if (priv->port[id].is_4p) {
  234. chan = priv->port[id].chan[1];
  235. val = tps23881_set_val(ret, chan, 0, BIT(chan % 4),
  236. BIT(chan % 4));
  237. val = tps23881_set_val(val, chan, 4, BIT(chan % 4),
  238. BIT(chan % 4));
  239. }
  240. ret = i2c_smbus_write_word_data(client, TPS23881_REG_DET_CLA_EN, val);
  241. if (ret)
  242. return ret;
  243. /* No power policy */
  244. if (priv->port[id].pw_pol < 0)
  245. return 0;
  246. ret = tps23881_pi_enable_manual_pol(priv, id);
  247. if (ret < 0)
  248. return ret;
  249. /* Set power policy */
  250. return tps23881_pi_set_pw_pol_limit(priv, id, priv->port[id].pw_pol,
  251. priv->port[id].is_4p);
  252. }
  253. static int
  254. tps23881_pi_get_admin_state(struct pse_controller_dev *pcdev, int id,
  255. struct pse_admin_state *admin_state)
  256. {
  257. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  258. struct i2c_client *client = priv->client;
  259. bool enabled;
  260. u8 chan;
  261. u16 val;
  262. int ret;
  263. ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
  264. if (ret < 0)
  265. return ret;
  266. chan = priv->port[id].chan[0];
  267. val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
  268. enabled = !!(val);
  269. if (priv->port[id].is_4p) {
  270. chan = priv->port[id].chan[1];
  271. val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
  272. enabled &= !!(val);
  273. }
  274. /* Return enabled status only if both channel are on this state */
  275. if (enabled)
  276. admin_state->c33_admin_state =
  277. ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED;
  278. else
  279. admin_state->c33_admin_state =
  280. ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED;
  281. return 0;
  282. }
  283. static int
  284. tps23881_pi_get_pw_status(struct pse_controller_dev *pcdev, int id,
  285. struct pse_pw_status *pw_status)
  286. {
  287. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  288. struct i2c_client *client = priv->client;
  289. bool delivering;
  290. u8 chan;
  291. u16 val;
  292. int ret;
  293. ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
  294. if (ret < 0)
  295. return ret;
  296. chan = priv->port[id].chan[0];
  297. val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
  298. delivering = !!(val);
  299. if (priv->port[id].is_4p) {
  300. chan = priv->port[id].chan[1];
  301. val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
  302. delivering &= !!(val);
  303. }
  304. /* Return delivering status only if both channel are on this state */
  305. if (delivering)
  306. pw_status->c33_pw_status =
  307. ETHTOOL_C33_PSE_PW_D_STATUS_DELIVERING;
  308. else
  309. pw_status->c33_pw_status =
  310. ETHTOOL_C33_PSE_PW_D_STATUS_DISABLED;
  311. return 0;
  312. }
  313. static int tps23881_pi_get_voltage(struct pse_controller_dev *pcdev, int id)
  314. {
  315. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  316. struct i2c_client *client = priv->client;
  317. int ret;
  318. u64 uV;
  319. ret = i2c_smbus_read_word_data(client, TPS23881_REG_INPUT_V);
  320. if (ret < 0)
  321. return ret;
  322. uV = ret & 0x3fff;
  323. uV *= TPS23881_UV_STEP;
  324. return (int)uV;
  325. }
  326. static int
  327. tps23881_pi_get_chan_current(struct tps23881_priv *priv, u8 chan)
  328. {
  329. struct i2c_client *client = priv->client;
  330. int reg, ret;
  331. u64 tmp_64;
  332. /* Registers 0x30 to 0x3d */
  333. reg = TPS23881_REG_CHAN1_A + (chan % 4) * 4 + (chan >= 4);
  334. ret = i2c_smbus_read_word_data(client, reg);
  335. if (ret < 0)
  336. return ret;
  337. tmp_64 = ret & 0x3fff;
  338. tmp_64 *= TPS23881_NA_STEP;
  339. /* uA = nA / 1000 */
  340. tmp_64 = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000);
  341. return (int)tmp_64;
  342. }
  343. static int tps23881_pi_get_pw_class(struct pse_controller_dev *pcdev,
  344. int id)
  345. {
  346. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  347. struct i2c_client *client = priv->client;
  348. int ret, reg;
  349. u8 chan;
  350. chan = priv->port[id].chan[0];
  351. reg = TPS23881_REG_CHAN1_CLASS + (chan % 4);
  352. ret = i2c_smbus_read_word_data(client, reg);
  353. if (ret < 0)
  354. return ret;
  355. return tps23881_calc_val(ret, chan, 4, 0x0f);
  356. }
  357. static int
  358. tps23881_pi_get_actual_pw(struct pse_controller_dev *pcdev, int id)
  359. {
  360. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  361. int ret, uV, uA;
  362. u64 tmp_64;
  363. u8 chan;
  364. ret = tps23881_pi_get_voltage(&priv->pcdev, id);
  365. if (ret < 0)
  366. return ret;
  367. uV = ret;
  368. chan = priv->port[id].chan[0];
  369. ret = tps23881_pi_get_chan_current(priv, chan);
  370. if (ret < 0)
  371. return ret;
  372. uA = ret;
  373. if (priv->port[id].is_4p) {
  374. chan = priv->port[id].chan[1];
  375. ret = tps23881_pi_get_chan_current(priv, chan);
  376. if (ret < 0)
  377. return ret;
  378. uA += ret;
  379. }
  380. tmp_64 = uV;
  381. tmp_64 *= uA;
  382. /* mW = uV * uA / 1000000000 */
  383. return DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
  384. }
  385. static int
  386. tps23881_pi_get_pw_limit_chan(struct tps23881_priv *priv, u8 chan)
  387. {
  388. struct i2c_client *client = priv->client;
  389. int ret, reg;
  390. u16 val;
  391. reg = TPS23881_REG_2PAIR_POL1 + (chan % 4);
  392. ret = i2c_smbus_read_word_data(client, reg);
  393. if (ret < 0)
  394. return ret;
  395. val = tps23881_calc_val(ret, chan, 0, 0xff);
  396. return val * TPS23881_MW_STEP;
  397. }
  398. static int tps23881_pi_get_pw_limit(struct pse_controller_dev *pcdev, int id)
  399. {
  400. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  401. int ret, mW;
  402. u8 chan;
  403. chan = priv->port[id].chan[0];
  404. ret = tps23881_pi_get_pw_limit_chan(priv, chan);
  405. if (ret < 0)
  406. return ret;
  407. mW = ret;
  408. if (priv->port[id].is_4p) {
  409. chan = priv->port[id].chan[1];
  410. ret = tps23881_pi_get_pw_limit_chan(priv, chan);
  411. if (ret < 0)
  412. return ret;
  413. mW += ret;
  414. }
  415. return mW;
  416. }
  417. static int tps23881_pi_set_pw_limit(struct pse_controller_dev *pcdev,
  418. int id, int max_mW)
  419. {
  420. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  421. u8 pw_pol;
  422. int ret;
  423. if (max_mW < TPS23881_MIN_PI_PW_LIMIT_MW || MAX_PI_PW < max_mW) {
  424. dev_err(&priv->client->dev,
  425. "power limit %d out of ranges [%d,%d]",
  426. max_mW, TPS23881_MIN_PI_PW_LIMIT_MW, MAX_PI_PW);
  427. return -ERANGE;
  428. }
  429. ret = tps23881_pi_enable_manual_pol(priv, id);
  430. if (ret < 0)
  431. return ret;
  432. pw_pol = DIV_ROUND_CLOSEST_ULL(max_mW, TPS23881_MW_STEP);
  433. /* Save power policy to reconfigure it after a disabled call */
  434. priv->port[id].pw_pol = pw_pol;
  435. return tps23881_pi_set_pw_pol_limit(priv, id, pw_pol,
  436. priv->port[id].is_4p);
  437. }
  438. static int
  439. tps23881_pi_get_pw_limit_ranges(struct pse_controller_dev *pcdev, int id,
  440. struct pse_pw_limit_ranges *pw_limit_ranges)
  441. {
  442. struct ethtool_c33_pse_pw_limit_range *c33_pw_limit_ranges;
  443. c33_pw_limit_ranges = kzalloc_obj(*c33_pw_limit_ranges);
  444. if (!c33_pw_limit_ranges)
  445. return -ENOMEM;
  446. c33_pw_limit_ranges->min = TPS23881_MIN_PI_PW_LIMIT_MW;
  447. c33_pw_limit_ranges->max = MAX_PI_PW;
  448. pw_limit_ranges->c33_pw_limit_ranges = c33_pw_limit_ranges;
  449. /* Return the number of ranges */
  450. return 1;
  451. }
  452. /* Parse managers subnode into a array of device node */
  453. static int
  454. tps23881_get_of_channels(struct tps23881_priv *priv,
  455. struct device_node *chan_node[TPS23881_MAX_CHANS])
  456. {
  457. struct device_node *channels_node, *node;
  458. int i, ret;
  459. if (!priv->np)
  460. return -EINVAL;
  461. channels_node = of_find_node_by_name(priv->np, "channels");
  462. if (!channels_node)
  463. return -EINVAL;
  464. for_each_child_of_node(channels_node, node) {
  465. u32 chan_id;
  466. if (!of_node_name_eq(node, "channel"))
  467. continue;
  468. ret = of_property_read_u32(node, "reg", &chan_id);
  469. if (ret) {
  470. ret = -EINVAL;
  471. goto out;
  472. }
  473. if (chan_id >= TPS23881_MAX_CHANS || chan_node[chan_id]) {
  474. dev_err(&priv->client->dev,
  475. "wrong number of port (%d)\n", chan_id);
  476. ret = -EINVAL;
  477. goto out;
  478. }
  479. of_node_get(node);
  480. chan_node[chan_id] = node;
  481. }
  482. of_node_put(channels_node);
  483. return 0;
  484. out:
  485. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  486. of_node_put(chan_node[i]);
  487. chan_node[i] = NULL;
  488. }
  489. of_node_put(node);
  490. of_node_put(channels_node);
  491. return ret;
  492. }
  493. struct tps23881_port_matrix {
  494. u8 pi_id;
  495. u8 lgcl_chan[2];
  496. u8 hw_chan[2];
  497. bool is_4p;
  498. bool exist;
  499. };
  500. static int
  501. tps23881_match_channel(const struct pse_pi_pairset *pairset,
  502. struct device_node *chan_node[TPS23881_MAX_CHANS])
  503. {
  504. int i;
  505. /* Look on every channels */
  506. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  507. if (pairset->np == chan_node[i])
  508. return i;
  509. }
  510. return -ENODEV;
  511. }
  512. static bool
  513. tps23881_is_chan_free(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
  514. int chan)
  515. {
  516. int i;
  517. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  518. if (port_matrix[i].exist &&
  519. (port_matrix[i].hw_chan[0] == chan ||
  520. port_matrix[i].hw_chan[1] == chan))
  521. return false;
  522. }
  523. return true;
  524. }
  525. /* Fill port matrix with the matching channels */
  526. static int
  527. tps23881_match_port_matrix(struct pse_pi *pi, int pi_id,
  528. struct device_node *chan_node[TPS23881_MAX_CHANS],
  529. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
  530. {
  531. int ret;
  532. if (!pi->pairset[0].np)
  533. return 0;
  534. ret = tps23881_match_channel(&pi->pairset[0], chan_node);
  535. if (ret < 0)
  536. return ret;
  537. if (!tps23881_is_chan_free(port_matrix, ret)) {
  538. pr_err("tps23881: channel %d already used\n", ret);
  539. return -ENODEV;
  540. }
  541. port_matrix[pi_id].hw_chan[0] = ret;
  542. port_matrix[pi_id].exist = true;
  543. if (!pi->pairset[1].np)
  544. return 0;
  545. ret = tps23881_match_channel(&pi->pairset[1], chan_node);
  546. if (ret < 0)
  547. return ret;
  548. if (!tps23881_is_chan_free(port_matrix, ret)) {
  549. pr_err("tps23881: channel %d already used\n", ret);
  550. return -ENODEV;
  551. }
  552. if (port_matrix[pi_id].hw_chan[0] / 4 != ret / 4) {
  553. pr_err("tps23881: 4-pair PSE can only be set within the same 4 ports group");
  554. return -ENODEV;
  555. }
  556. port_matrix[pi_id].hw_chan[1] = ret;
  557. port_matrix[pi_id].is_4p = true;
  558. return 0;
  559. }
  560. static int
  561. tps23881_get_unused_chan(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
  562. int port_cnt)
  563. {
  564. bool used;
  565. int i, j;
  566. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  567. used = false;
  568. for (j = 0; j < port_cnt; j++) {
  569. if (port_matrix[j].hw_chan[0] == i) {
  570. used = true;
  571. break;
  572. }
  573. if (port_matrix[j].is_4p &&
  574. port_matrix[j].hw_chan[1] == i) {
  575. used = true;
  576. break;
  577. }
  578. }
  579. if (!used)
  580. return i;
  581. }
  582. return -ENODEV;
  583. }
  584. /* Sort the port matrix to following particular hardware ports matrix
  585. * specification of the tps23881. The device has two 4-ports groups and
  586. * each 4-pair powered device has to be configured to use two consecutive
  587. * logical channel in each 4 ports group (1 and 2 or 3 and 4). Also the
  588. * hardware matrix has to be fully configured even with unused chan to be
  589. * valid.
  590. */
  591. static int
  592. tps23881_sort_port_matrix(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
  593. {
  594. struct tps23881_port_matrix tmp_port_matrix[TPS23881_MAX_CHANS] = {0};
  595. int i, ret, port_cnt = 0, cnt_4ch_grp1 = 0, cnt_4ch_grp2 = 4;
  596. /* Configure 4p port matrix */
  597. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  598. int *cnt;
  599. if (!port_matrix[i].exist || !port_matrix[i].is_4p)
  600. continue;
  601. if (port_matrix[i].hw_chan[0] < 4)
  602. cnt = &cnt_4ch_grp1;
  603. else
  604. cnt = &cnt_4ch_grp2;
  605. tmp_port_matrix[port_cnt].exist = true;
  606. tmp_port_matrix[port_cnt].is_4p = true;
  607. tmp_port_matrix[port_cnt].pi_id = i;
  608. tmp_port_matrix[port_cnt].hw_chan[0] = port_matrix[i].hw_chan[0];
  609. tmp_port_matrix[port_cnt].hw_chan[1] = port_matrix[i].hw_chan[1];
  610. /* 4-pair ports have to be configured with consecutive
  611. * logical channels 0 and 1, 2 and 3.
  612. */
  613. tmp_port_matrix[port_cnt].lgcl_chan[0] = (*cnt)++;
  614. tmp_port_matrix[port_cnt].lgcl_chan[1] = (*cnt)++;
  615. port_cnt++;
  616. }
  617. /* Configure 2p port matrix */
  618. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  619. int *cnt;
  620. if (!port_matrix[i].exist || port_matrix[i].is_4p)
  621. continue;
  622. if (port_matrix[i].hw_chan[0] < 4)
  623. cnt = &cnt_4ch_grp1;
  624. else
  625. cnt = &cnt_4ch_grp2;
  626. tmp_port_matrix[port_cnt].exist = true;
  627. tmp_port_matrix[port_cnt].pi_id = i;
  628. tmp_port_matrix[port_cnt].lgcl_chan[0] = (*cnt)++;
  629. tmp_port_matrix[port_cnt].hw_chan[0] = port_matrix[i].hw_chan[0];
  630. port_cnt++;
  631. }
  632. /* Complete the rest of the first 4 port group matrix even if
  633. * channels are unused
  634. */
  635. while (cnt_4ch_grp1 < 4) {
  636. ret = tps23881_get_unused_chan(tmp_port_matrix, port_cnt);
  637. if (ret < 0) {
  638. pr_err("tps23881: port matrix issue, no chan available\n");
  639. return ret;
  640. }
  641. if (port_cnt >= TPS23881_MAX_CHANS) {
  642. pr_err("tps23881: wrong number of channels\n");
  643. return -ENODEV;
  644. }
  645. tmp_port_matrix[port_cnt].lgcl_chan[0] = cnt_4ch_grp1;
  646. tmp_port_matrix[port_cnt].hw_chan[0] = ret;
  647. cnt_4ch_grp1++;
  648. port_cnt++;
  649. }
  650. /* Complete the rest of the second 4 port group matrix even if
  651. * channels are unused
  652. */
  653. while (cnt_4ch_grp2 < 8) {
  654. ret = tps23881_get_unused_chan(tmp_port_matrix, port_cnt);
  655. if (ret < 0) {
  656. pr_err("tps23881: port matrix issue, no chan available\n");
  657. return -ENODEV;
  658. }
  659. if (port_cnt >= TPS23881_MAX_CHANS) {
  660. pr_err("tps23881: wrong number of channels\n");
  661. return -ENODEV;
  662. }
  663. tmp_port_matrix[port_cnt].lgcl_chan[0] = cnt_4ch_grp2;
  664. tmp_port_matrix[port_cnt].hw_chan[0] = ret;
  665. cnt_4ch_grp2++;
  666. port_cnt++;
  667. }
  668. memcpy(port_matrix, tmp_port_matrix, sizeof(tmp_port_matrix));
  669. return port_cnt;
  670. }
  671. /* Write port matrix to the hardware port matrix and the software port
  672. * matrix.
  673. */
  674. static int
  675. tps23881_write_port_matrix(struct tps23881_priv *priv,
  676. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
  677. int port_cnt)
  678. {
  679. struct i2c_client *client = priv->client;
  680. u8 pi_id, lgcl_chan, hw_chan;
  681. u16 val = 0;
  682. int i;
  683. for (i = 0; i < port_cnt; i++) {
  684. pi_id = port_matrix[i].pi_id;
  685. lgcl_chan = port_matrix[i].lgcl_chan[0];
  686. hw_chan = port_matrix[i].hw_chan[0] % 4;
  687. /* Set software port matrix for existing ports */
  688. if (port_matrix[i].exist) {
  689. priv->port[pi_id].chan[0] = lgcl_chan;
  690. priv->port[pi_id].exist = true;
  691. }
  692. /* Initialize power policy internal value */
  693. priv->port[pi_id].pw_pol = -1;
  694. /* Set hardware port matrix for all ports */
  695. val |= hw_chan << (lgcl_chan * 2);
  696. if (!port_matrix[i].is_4p)
  697. continue;
  698. lgcl_chan = port_matrix[i].lgcl_chan[1];
  699. hw_chan = port_matrix[i].hw_chan[1] % 4;
  700. /* Set software port matrix for existing ports */
  701. if (port_matrix[i].exist) {
  702. priv->port[pi_id].is_4p = true;
  703. priv->port[pi_id].chan[1] = lgcl_chan;
  704. }
  705. /* Set hardware port matrix for all ports */
  706. val |= hw_chan << (lgcl_chan * 2);
  707. }
  708. /* Write hardware ports matrix */
  709. return i2c_smbus_write_word_data(client, TPS23881_REG_PORT_MAP, val);
  710. }
  711. static int
  712. tps23881_set_ports_conf(struct tps23881_priv *priv,
  713. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
  714. {
  715. struct i2c_client *client = priv->client;
  716. int i, ret;
  717. u16 val;
  718. /* Set operating mode */
  719. ret = i2c_smbus_write_word_data(client, TPS23881_REG_OP_MODE,
  720. TPS23881_OP_MODE_SEMIAUTO);
  721. if (ret)
  722. return ret;
  723. /* Disable DC disconnect */
  724. ret = i2c_smbus_write_word_data(client, TPS23881_REG_DIS_EN, 0x0);
  725. if (ret)
  726. return ret;
  727. /* Set port power allocation */
  728. val = 0;
  729. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  730. if (!port_matrix[i].exist)
  731. continue;
  732. if (port_matrix[i].is_4p)
  733. val |= 0xf << ((port_matrix[i].lgcl_chan[0] / 2) * 4);
  734. else
  735. val |= 0x3 << ((port_matrix[i].lgcl_chan[0] / 2) * 4);
  736. }
  737. ret = i2c_smbus_write_word_data(client, TPS23881_REG_PORT_POWER, val);
  738. if (ret)
  739. return ret;
  740. /* Enable detection and classification */
  741. val = 0;
  742. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  743. if (!port_matrix[i].exist)
  744. continue;
  745. val |= BIT(port_matrix[i].lgcl_chan[0]) |
  746. BIT(port_matrix[i].lgcl_chan[0] + 4);
  747. if (port_matrix[i].is_4p)
  748. val |= BIT(port_matrix[i].lgcl_chan[1]) |
  749. BIT(port_matrix[i].lgcl_chan[1] + 4);
  750. }
  751. return i2c_smbus_write_word_data(client, TPS23881_REG_DET_CLA_EN, val);
  752. }
  753. static int
  754. tps23881_set_ports_matrix(struct tps23881_priv *priv,
  755. struct device_node *chan_node[TPS23881_MAX_CHANS])
  756. {
  757. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS] = {0};
  758. int i, ret;
  759. /* Update with values for every PSE PIs */
  760. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  761. ret = tps23881_match_port_matrix(&priv->pcdev.pi[i], i,
  762. chan_node, port_matrix);
  763. if (ret)
  764. return ret;
  765. }
  766. ret = tps23881_sort_port_matrix(port_matrix);
  767. if (ret < 0)
  768. return ret;
  769. ret = tps23881_write_port_matrix(priv, port_matrix, ret);
  770. if (ret)
  771. return ret;
  772. return tps23881_set_ports_conf(priv, port_matrix);
  773. }
  774. static int tps23881_setup_pi_matrix(struct pse_controller_dev *pcdev)
  775. {
  776. struct device_node *chan_node[TPS23881_MAX_CHANS] = {NULL};
  777. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  778. int ret, i;
  779. ret = tps23881_get_of_channels(priv, chan_node);
  780. if (ret < 0) {
  781. dev_warn(&priv->client->dev,
  782. "Unable to parse port-matrix, default matrix will be used\n");
  783. return 0;
  784. }
  785. ret = tps23881_set_ports_matrix(priv, chan_node);
  786. for (i = 0; i < TPS23881_MAX_CHANS; i++)
  787. of_node_put(chan_node[i]);
  788. return ret;
  789. }
  790. static int tps23881_power_class_table[] = {
  791. -ERANGE,
  792. 4000,
  793. 7000,
  794. 15500,
  795. 30000,
  796. 15500,
  797. 15500,
  798. -ERANGE,
  799. 45000,
  800. 60000,
  801. 75000,
  802. 90000,
  803. 15500,
  804. 45000,
  805. -ERANGE,
  806. -ERANGE,
  807. };
  808. static int tps23881_pi_get_pw_req(struct pse_controller_dev *pcdev, int id)
  809. {
  810. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  811. struct i2c_client *client = priv->client;
  812. u8 reg, chan;
  813. int ret;
  814. u16 val;
  815. /* For a 4-pair the classification need 5ms to be completed */
  816. if (priv->port[id].is_4p)
  817. mdelay(5);
  818. chan = priv->port[id].chan[0];
  819. reg = TPS23881_REG_DISC + (chan % 4);
  820. ret = i2c_smbus_read_word_data(client, reg);
  821. if (ret < 0)
  822. return ret;
  823. val = tps23881_calc_val(ret, chan, 4, 0xf);
  824. return tps23881_power_class_table[val];
  825. }
  826. static const struct pse_controller_ops tps23881_ops = {
  827. .setup_pi_matrix = tps23881_setup_pi_matrix,
  828. .pi_enable = tps23881_pi_enable,
  829. .pi_disable = tps23881_pi_disable,
  830. .pi_get_admin_state = tps23881_pi_get_admin_state,
  831. .pi_get_pw_status = tps23881_pi_get_pw_status,
  832. .pi_get_pw_class = tps23881_pi_get_pw_class,
  833. .pi_get_actual_pw = tps23881_pi_get_actual_pw,
  834. .pi_get_voltage = tps23881_pi_get_voltage,
  835. .pi_get_pw_limit = tps23881_pi_get_pw_limit,
  836. .pi_set_pw_limit = tps23881_pi_set_pw_limit,
  837. .pi_get_pw_limit_ranges = tps23881_pi_get_pw_limit_ranges,
  838. .pi_get_pw_req = tps23881_pi_get_pw_req,
  839. };
  840. struct tps23881_info {
  841. u8 dev_id; /* device ID and silicon revision */
  842. const char *fw_parity_name; /* parity code firmware file name */
  843. const char *fw_sram_name; /* SRAM code firmware file name */
  844. };
  845. enum tps23881_model {
  846. TPS23881,
  847. TPS23881B,
  848. };
  849. static const struct tps23881_info tps23881_info[] = {
  850. [TPS23881] = {
  851. .dev_id = 0x22,
  852. .fw_parity_name = "ti/tps23881/tps23881-parity-14.bin",
  853. .fw_sram_name = "ti/tps23881/tps23881-sram-14.bin",
  854. },
  855. [TPS23881B] = {
  856. .dev_id = 0x24,
  857. /* skip SRAM load, ROM provides Clause 145 hardware-level support */
  858. },
  859. };
  860. struct tps23881_fw_conf {
  861. u8 reg;
  862. u8 val;
  863. };
  864. static const struct tps23881_fw_conf tps23881_fw_parity_conf[] = {
  865. {.reg = 0x60, .val = 0x01},
  866. {.reg = 0x62, .val = 0x00},
  867. {.reg = 0x63, .val = 0x80},
  868. {.reg = 0x60, .val = 0xC4},
  869. {.reg = 0x1D, .val = 0xBC},
  870. {.reg = 0xD7, .val = 0x02},
  871. {.reg = 0x91, .val = 0x00},
  872. {.reg = 0x90, .val = 0x00},
  873. {.reg = 0xD7, .val = 0x00},
  874. {.reg = 0x1D, .val = 0x00},
  875. { /* sentinel */ }
  876. };
  877. static const struct tps23881_fw_conf tps23881_fw_sram_conf[] = {
  878. {.reg = 0x60, .val = 0xC5},
  879. {.reg = 0x62, .val = 0x00},
  880. {.reg = 0x63, .val = 0x80},
  881. {.reg = 0x60, .val = 0xC0},
  882. {.reg = 0x1D, .val = 0xBC},
  883. {.reg = 0xD7, .val = 0x02},
  884. {.reg = 0x91, .val = 0x00},
  885. {.reg = 0x90, .val = 0x00},
  886. {.reg = 0xD7, .val = 0x00},
  887. {.reg = 0x1D, .val = 0x00},
  888. { /* sentinel */ }
  889. };
  890. static int tps23881_flash_sram_fw_part(struct i2c_client *client,
  891. const char *fw_name,
  892. const struct tps23881_fw_conf *fw_conf)
  893. {
  894. const struct firmware *fw = NULL;
  895. int i, ret;
  896. ret = request_firmware(&fw, fw_name, &client->dev);
  897. if (ret)
  898. return ret;
  899. dev_dbg(&client->dev, "Flashing %s\n", fw_name);
  900. /* Prepare device for RAM download */
  901. while (fw_conf->reg) {
  902. ret = i2c_smbus_write_byte_data(client, fw_conf->reg,
  903. fw_conf->val);
  904. if (ret)
  905. goto out;
  906. fw_conf++;
  907. }
  908. /* Flash the firmware file */
  909. for (i = 0; i < fw->size; i++) {
  910. ret = i2c_smbus_write_byte_data(client,
  911. TPS23881_REG_SRAM_DATA,
  912. fw->data[i]);
  913. if (ret)
  914. goto out;
  915. }
  916. out:
  917. release_firmware(fw);
  918. return ret;
  919. }
  920. static int tps23881_flash_sram_fw(struct i2c_client *client,
  921. const struct tps23881_info *info)
  922. {
  923. int ret;
  924. ret = tps23881_flash_sram_fw_part(client, info->fw_parity_name,
  925. tps23881_fw_parity_conf);
  926. if (ret)
  927. return ret;
  928. ret = tps23881_flash_sram_fw_part(client, info->fw_sram_name,
  929. tps23881_fw_sram_conf);
  930. if (ret)
  931. return ret;
  932. ret = i2c_smbus_write_byte_data(client, TPS23881_REG_SRAM_CTRL, 0x18);
  933. if (ret)
  934. return ret;
  935. mdelay(12);
  936. return 0;
  937. }
  938. /* Convert interrupt events to 0xff to be aligned with the chan
  939. * number.
  940. */
  941. static u8 tps23881_irq_export_chans_helper(u16 reg_val, u8 field_offset)
  942. {
  943. u8 val;
  944. val = (reg_val >> (4 + field_offset) & 0xf0) |
  945. (reg_val >> field_offset & 0x0f);
  946. return val;
  947. }
  948. /* Convert chan number to port number */
  949. static void tps23881_set_notifs_helper(struct tps23881_priv *priv,
  950. u8 chans,
  951. unsigned long *notifs,
  952. unsigned long *notifs_mask,
  953. enum ethtool_pse_event event)
  954. {
  955. u8 chan;
  956. int i;
  957. if (!chans)
  958. return;
  959. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  960. if (!priv->port[i].exist)
  961. continue;
  962. /* No need to look at the 2nd channel in case of PoE4 as
  963. * both registers are set.
  964. */
  965. chan = priv->port[i].chan[0];
  966. if (BIT(chan) & chans) {
  967. *notifs_mask |= BIT(i);
  968. notifs[i] |= event;
  969. }
  970. }
  971. }
  972. static void tps23881_irq_event_over_temp(struct tps23881_priv *priv,
  973. u16 reg_val,
  974. unsigned long *notifs,
  975. unsigned long *notifs_mask)
  976. {
  977. int i;
  978. if (reg_val & TPS23881_REG_TSD) {
  979. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  980. if (!priv->port[i].exist)
  981. continue;
  982. *notifs_mask |= BIT(i);
  983. notifs[i] |= ETHTOOL_PSE_EVENT_OVER_TEMP;
  984. }
  985. }
  986. }
  987. static int tps23881_irq_event_over_current(struct tps23881_priv *priv,
  988. u16 reg_val,
  989. unsigned long *notifs,
  990. unsigned long *notifs_mask)
  991. {
  992. int i, ret;
  993. u8 chans;
  994. chans = tps23881_irq_export_chans_helper(reg_val, 0);
  995. if (!chans)
  996. return 0;
  997. tps23881_set_notifs_helper(priv, chans, notifs, notifs_mask,
  998. ETHTOOL_PSE_EVENT_OVER_CURRENT |
  999. ETHTOOL_C33_PSE_EVENT_DISCONNECTION);
  1000. /* Over Current event resets the power limit registers so we need
  1001. * to configured it again.
  1002. */
  1003. for_each_set_bit(i, notifs_mask, priv->pcdev.nr_lines) {
  1004. if (priv->port[i].pw_pol < 0)
  1005. continue;
  1006. ret = tps23881_pi_enable_manual_pol(priv, i);
  1007. if (ret < 0)
  1008. return ret;
  1009. /* Set power policy */
  1010. ret = tps23881_pi_set_pw_pol_limit(priv, i,
  1011. priv->port[i].pw_pol,
  1012. priv->port[i].is_4p);
  1013. if (ret < 0)
  1014. return ret;
  1015. }
  1016. return 0;
  1017. }
  1018. static void tps23881_irq_event_disconnection(struct tps23881_priv *priv,
  1019. u16 reg_val,
  1020. unsigned long *notifs,
  1021. unsigned long *notifs_mask)
  1022. {
  1023. u8 chans;
  1024. chans = tps23881_irq_export_chans_helper(reg_val, 4);
  1025. if (chans)
  1026. tps23881_set_notifs_helper(priv, chans, notifs, notifs_mask,
  1027. ETHTOOL_C33_PSE_EVENT_DISCONNECTION);
  1028. }
  1029. static int tps23881_irq_event_detection(struct tps23881_priv *priv,
  1030. u16 reg_val,
  1031. unsigned long *notifs,
  1032. unsigned long *notifs_mask)
  1033. {
  1034. enum ethtool_pse_event event;
  1035. int reg, ret, i, val;
  1036. unsigned long chans;
  1037. chans = tps23881_irq_export_chans_helper(reg_val, 0);
  1038. for_each_set_bit(i, &chans, TPS23881_MAX_CHANS) {
  1039. reg = TPS23881_REG_DISC + (i % 4);
  1040. ret = i2c_smbus_read_word_data(priv->client, reg);
  1041. if (ret < 0)
  1042. return ret;
  1043. val = tps23881_calc_val(ret, i, 0, 0xf);
  1044. /* If detection valid */
  1045. if (val == 0x4)
  1046. event = ETHTOOL_C33_PSE_EVENT_DETECTION;
  1047. else
  1048. event = ETHTOOL_C33_PSE_EVENT_DISCONNECTION;
  1049. tps23881_set_notifs_helper(priv, BIT(i), notifs,
  1050. notifs_mask, event);
  1051. }
  1052. return 0;
  1053. }
  1054. static int tps23881_irq_event_classification(struct tps23881_priv *priv,
  1055. u16 reg_val,
  1056. unsigned long *notifs,
  1057. unsigned long *notifs_mask)
  1058. {
  1059. int reg, ret, val, i;
  1060. unsigned long chans;
  1061. chans = tps23881_irq_export_chans_helper(reg_val, 4);
  1062. for_each_set_bit(i, &chans, TPS23881_MAX_CHANS) {
  1063. reg = TPS23881_REG_DISC + (i % 4);
  1064. ret = i2c_smbus_read_word_data(priv->client, reg);
  1065. if (ret < 0)
  1066. return ret;
  1067. val = tps23881_calc_val(ret, i, 4, 0xf);
  1068. /* Do not report classification event for unknown class */
  1069. if (!val || val == 0x8 || val == 0xf)
  1070. continue;
  1071. tps23881_set_notifs_helper(priv, BIT(i), notifs,
  1072. notifs_mask,
  1073. ETHTOOL_C33_PSE_EVENT_CLASSIFICATION);
  1074. }
  1075. return 0;
  1076. }
  1077. static int tps23881_irq_event_handler(struct tps23881_priv *priv, u16 reg,
  1078. unsigned long *notifs,
  1079. unsigned long *notifs_mask)
  1080. {
  1081. struct i2c_client *client = priv->client;
  1082. int ret, val;
  1083. /* The Supply event bit is repeated twice so we only need to read
  1084. * the one from the first byte.
  1085. */
  1086. if (reg & TPS23881_REG_IT_SUPF) {
  1087. ret = i2c_smbus_read_word_data(client, TPS23881_REG_SUPF_EVENT);
  1088. if (ret < 0)
  1089. return ret;
  1090. tps23881_irq_event_over_temp(priv, ret, notifs, notifs_mask);
  1091. }
  1092. if (reg & (TPS23881_REG_IT_IFAULT | TPS23881_REG_IT_IFAULT << 8 |
  1093. TPS23881_REG_IT_DISF | TPS23881_REG_IT_DISF << 8)) {
  1094. ret = i2c_smbus_read_word_data(client, TPS23881_REG_FAULT);
  1095. if (ret < 0)
  1096. return ret;
  1097. ret = tps23881_irq_event_over_current(priv, ret, notifs,
  1098. notifs_mask);
  1099. if (ret)
  1100. return ret;
  1101. tps23881_irq_event_disconnection(priv, ret, notifs, notifs_mask);
  1102. }
  1103. if (reg & (TPS23881_REG_IT_DETC | TPS23881_REG_IT_DETC << 8 |
  1104. TPS23881_REG_IT_CLASC | TPS23881_REG_IT_CLASC << 8)) {
  1105. ret = i2c_smbus_read_word_data(client, TPS23881_REG_DET_EVENT);
  1106. if (ret < 0)
  1107. return ret;
  1108. val = ret;
  1109. ret = tps23881_irq_event_detection(priv, val, notifs,
  1110. notifs_mask);
  1111. if (ret)
  1112. return ret;
  1113. ret = tps23881_irq_event_classification(priv, val, notifs,
  1114. notifs_mask);
  1115. if (ret)
  1116. return ret;
  1117. }
  1118. return 0;
  1119. }
  1120. static int tps23881_irq_handler(int irq, struct pse_controller_dev *pcdev,
  1121. unsigned long *notifs,
  1122. unsigned long *notifs_mask)
  1123. {
  1124. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  1125. struct i2c_client *client = priv->client;
  1126. int ret, it_mask, retry;
  1127. /* Get interruption mask */
  1128. ret = i2c_smbus_read_word_data(client, TPS23881_REG_IT_MASK);
  1129. if (ret < 0)
  1130. return ret;
  1131. it_mask = ret;
  1132. /* Read interrupt register until it frees the interruption pin. */
  1133. retry = 0;
  1134. while (true) {
  1135. if (retry > TPS23881_MAX_IRQ_RETRIES) {
  1136. dev_err(&client->dev, "interrupt never freed");
  1137. return -ETIMEDOUT;
  1138. }
  1139. ret = i2c_smbus_read_word_data(client, TPS23881_REG_IT);
  1140. if (ret < 0)
  1141. return ret;
  1142. /* No more relevant interruption */
  1143. if (!(ret & it_mask))
  1144. return 0;
  1145. ret = tps23881_irq_event_handler(priv, (u16)ret, notifs,
  1146. notifs_mask);
  1147. if (ret)
  1148. return ret;
  1149. retry++;
  1150. }
  1151. return 0;
  1152. }
  1153. static int tps23881_setup_irq(struct tps23881_priv *priv, int irq)
  1154. {
  1155. struct i2c_client *client = priv->client;
  1156. struct pse_irq_desc irq_desc = {
  1157. .name = "tps23881-irq",
  1158. .map_event = tps23881_irq_handler,
  1159. };
  1160. int ret;
  1161. u16 val;
  1162. if (!irq) {
  1163. dev_err(&client->dev, "interrupt is missing");
  1164. return -EINVAL;
  1165. }
  1166. val = TPS23881_REG_IT_IFAULT | TPS23881_REG_IT_SUPF |
  1167. TPS23881_REG_IT_DETC | TPS23881_REG_IT_CLASC |
  1168. TPS23881_REG_IT_DISF;
  1169. val |= val << 8;
  1170. ret = i2c_smbus_write_word_data(client, TPS23881_REG_IT_MASK, val);
  1171. if (ret)
  1172. return ret;
  1173. ret = i2c_smbus_read_word_data(client, TPS23881_REG_GEN_MASK);
  1174. if (ret < 0)
  1175. return ret;
  1176. val = TPS23881_REG_INTEN | TPS23881_REG_CLCHE | TPS23881_REG_DECHE;
  1177. val |= val << 8;
  1178. val |= (u16)ret;
  1179. ret = i2c_smbus_write_word_data(client, TPS23881_REG_GEN_MASK, val);
  1180. if (ret < 0)
  1181. return ret;
  1182. /* Reset interrupts registers */
  1183. ret = i2c_smbus_write_word_data(client, TPS23881_REG_RESET,
  1184. TPS23881_REG_CLRAIN);
  1185. if (ret < 0)
  1186. return ret;
  1187. return devm_pse_irq_helper(&priv->pcdev, irq, 0, &irq_desc);
  1188. }
  1189. static int tps23881_i2c_probe(struct i2c_client *client)
  1190. {
  1191. struct device *dev = &client->dev;
  1192. const struct tps23881_info *info;
  1193. struct tps23881_priv *priv;
  1194. struct gpio_desc *reset;
  1195. int ret;
  1196. u8 val;
  1197. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1198. dev_err(dev, "i2c check functionality failed\n");
  1199. return -ENXIO;
  1200. }
  1201. info = i2c_get_match_data(client);
  1202. if (!info)
  1203. return -EINVAL;
  1204. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1205. if (!priv)
  1206. return -ENOMEM;
  1207. reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  1208. if (IS_ERR(reset))
  1209. return dev_err_probe(&client->dev, PTR_ERR(reset), "Failed to get reset GPIO\n");
  1210. if (reset) {
  1211. /* TPS23880 datasheet (Rev G) indicates minimum reset pulse is 5us */
  1212. usleep_range(5, 10);
  1213. gpiod_set_value_cansleep(reset, 0); /* De-assert reset */
  1214. /* TPS23880 datasheet indicates the minimum time after power on reset
  1215. * should be 20ms, but the document describing how to load SRAM ("How
  1216. * to Load TPS2388x SRAM and Parity Code over I2C" (Rev E))
  1217. * indicates we should delay that programming by at least 50ms. So
  1218. * we'll wait the entire 50ms here to ensure we're safe to go to the
  1219. * SRAM loading procedure.
  1220. */
  1221. msleep(50);
  1222. }
  1223. ret = i2c_smbus_read_byte_data(client, TPS23881_REG_DEVID);
  1224. if (ret < 0)
  1225. return ret;
  1226. if (ret != info->dev_id) {
  1227. dev_err(dev, "Wrong device ID\n");
  1228. return -ENXIO;
  1229. }
  1230. if (info->fw_sram_name) {
  1231. ret = tps23881_flash_sram_fw(client, info);
  1232. if (ret < 0)
  1233. return ret;
  1234. }
  1235. ret = i2c_smbus_read_byte_data(client, TPS23881_REG_FWREV);
  1236. if (ret < 0)
  1237. return ret;
  1238. if (ret == 0xFF) {
  1239. dev_err(&client->dev, "Device entered safe mode\n");
  1240. return -ENXIO;
  1241. }
  1242. dev_info(&client->dev, "Firmware revision 0x%x%s\n", ret,
  1243. ret == 0x00 ? " (ROM firmware)" : "");
  1244. /* Set configuration B, 16 bit access on a single device address */
  1245. ret = i2c_smbus_read_byte_data(client, TPS23881_REG_GEN_MASK);
  1246. if (ret < 0)
  1247. return ret;
  1248. val = ret | TPS23881_REG_NBITACC;
  1249. ret = i2c_smbus_write_byte_data(client, TPS23881_REG_GEN_MASK, val);
  1250. if (ret)
  1251. return ret;
  1252. priv->client = client;
  1253. i2c_set_clientdata(client, priv);
  1254. priv->np = dev->of_node;
  1255. priv->pcdev.owner = THIS_MODULE;
  1256. priv->pcdev.ops = &tps23881_ops;
  1257. priv->pcdev.dev = dev;
  1258. priv->pcdev.types = ETHTOOL_PSE_C33;
  1259. priv->pcdev.nr_lines = TPS23881_MAX_CHANS;
  1260. priv->pcdev.supp_budget_eval_strategies = PSE_BUDGET_EVAL_STRAT_STATIC;
  1261. ret = devm_pse_controller_register(dev, &priv->pcdev);
  1262. if (ret) {
  1263. return dev_err_probe(dev, ret,
  1264. "failed to register PSE controller\n");
  1265. }
  1266. ret = tps23881_setup_irq(priv, client->irq);
  1267. if (ret)
  1268. return ret;
  1269. return ret;
  1270. }
  1271. static const struct i2c_device_id tps23881_id[] = {
  1272. { "tps23881", .driver_data = (kernel_ulong_t)&tps23881_info[TPS23881] },
  1273. { "tps23881b", .driver_data = (kernel_ulong_t)&tps23881_info[TPS23881B] },
  1274. { }
  1275. };
  1276. MODULE_DEVICE_TABLE(i2c, tps23881_id);
  1277. static const struct of_device_id tps23881_of_match[] = {
  1278. {
  1279. .compatible = "ti,tps23881",
  1280. .data = &tps23881_info[TPS23881]
  1281. },
  1282. {
  1283. .compatible = "ti,tps23881b",
  1284. .data = &tps23881_info[TPS23881B]
  1285. },
  1286. { },
  1287. };
  1288. MODULE_DEVICE_TABLE(of, tps23881_of_match);
  1289. static struct i2c_driver tps23881_driver = {
  1290. .probe = tps23881_i2c_probe,
  1291. .id_table = tps23881_id,
  1292. .driver = {
  1293. .name = "tps23881",
  1294. .of_match_table = tps23881_of_match,
  1295. },
  1296. };
  1297. module_i2c_driver(tps23881_driver);
  1298. MODULE_AUTHOR("Kory Maincent <kory.maincent@bootlin.com>");
  1299. MODULE_DESCRIPTION("TI TPS23881 PoE PSE Controller driver");
  1300. MODULE_LICENSE("GPL");