88pm860x-core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * Base driver for Marvell 88PM8607
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/i2c.h>
  15. #include <linux/irq.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/of.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/mfd/88pm860x.h>
  25. #include <linux/regulator/machine.h>
  26. #include <linux/power/charger-manager.h>
  27. #define INT_STATUS_NUM 3
  28. static struct resource bk0_resources[] = {
  29. {2, 2, "duty cycle", IORESOURCE_REG, },
  30. {3, 3, "always on", IORESOURCE_REG, },
  31. {3, 3, "current", IORESOURCE_REG, },
  32. };
  33. static struct resource bk1_resources[] = {
  34. {4, 4, "duty cycle", IORESOURCE_REG, },
  35. {5, 5, "always on", IORESOURCE_REG, },
  36. {5, 5, "current", IORESOURCE_REG, },
  37. };
  38. static struct resource bk2_resources[] = {
  39. {6, 6, "duty cycle", IORESOURCE_REG, },
  40. {7, 7, "always on", IORESOURCE_REG, },
  41. {5, 5, "current", IORESOURCE_REG, },
  42. };
  43. static struct resource led0_resources[] = {
  44. /* RGB1 Red LED */
  45. {0xd, 0xd, "control", IORESOURCE_REG, },
  46. {0xc, 0xc, "blink", IORESOURCE_REG, },
  47. };
  48. static struct resource led1_resources[] = {
  49. /* RGB1 Green LED */
  50. {0xe, 0xe, "control", IORESOURCE_REG, },
  51. {0xc, 0xc, "blink", IORESOURCE_REG, },
  52. };
  53. static struct resource led2_resources[] = {
  54. /* RGB1 Blue LED */
  55. {0xf, 0xf, "control", IORESOURCE_REG, },
  56. {0xc, 0xc, "blink", IORESOURCE_REG, },
  57. };
  58. static struct resource led3_resources[] = {
  59. /* RGB2 Red LED */
  60. {0x9, 0x9, "control", IORESOURCE_REG, },
  61. {0x8, 0x8, "blink", IORESOURCE_REG, },
  62. };
  63. static struct resource led4_resources[] = {
  64. /* RGB2 Green LED */
  65. {0xa, 0xa, "control", IORESOURCE_REG, },
  66. {0x8, 0x8, "blink", IORESOURCE_REG, },
  67. };
  68. static struct resource led5_resources[] = {
  69. /* RGB2 Blue LED */
  70. {0xb, 0xb, "control", IORESOURCE_REG, },
  71. {0x8, 0x8, "blink", IORESOURCE_REG, },
  72. };
  73. static struct resource buck1_resources[] = {
  74. {0x24, 0x24, "buck set", IORESOURCE_REG, },
  75. };
  76. static struct resource buck2_resources[] = {
  77. {0x25, 0x25, "buck set", IORESOURCE_REG, },
  78. };
  79. static struct resource buck3_resources[] = {
  80. {0x26, 0x26, "buck set", IORESOURCE_REG, },
  81. };
  82. static struct resource ldo1_resources[] = {
  83. {0x10, 0x10, "ldo set", IORESOURCE_REG, },
  84. };
  85. static struct resource ldo2_resources[] = {
  86. {0x11, 0x11, "ldo set", IORESOURCE_REG, },
  87. };
  88. static struct resource ldo3_resources[] = {
  89. {0x12, 0x12, "ldo set", IORESOURCE_REG, },
  90. };
  91. static struct resource ldo4_resources[] = {
  92. {0x13, 0x13, "ldo set", IORESOURCE_REG, },
  93. };
  94. static struct resource ldo5_resources[] = {
  95. {0x14, 0x14, "ldo set", IORESOURCE_REG, },
  96. };
  97. static struct resource ldo6_resources[] = {
  98. {0x15, 0x15, "ldo set", IORESOURCE_REG, },
  99. };
  100. static struct resource ldo7_resources[] = {
  101. {0x16, 0x16, "ldo set", IORESOURCE_REG, },
  102. };
  103. static struct resource ldo8_resources[] = {
  104. {0x17, 0x17, "ldo set", IORESOURCE_REG, },
  105. };
  106. static struct resource ldo9_resources[] = {
  107. {0x18, 0x18, "ldo set", IORESOURCE_REG, },
  108. };
  109. static struct resource ldo10_resources[] = {
  110. {0x19, 0x19, "ldo set", IORESOURCE_REG, },
  111. };
  112. static struct resource ldo12_resources[] = {
  113. {0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
  114. };
  115. static struct resource ldo_vibrator_resources[] = {
  116. {0x28, 0x28, "ldo set", IORESOURCE_REG, },
  117. };
  118. static struct resource ldo14_resources[] = {
  119. {0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
  120. };
  121. static struct resource touch_resources[] = {
  122. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  123. };
  124. static struct resource onkey_resources[] = {
  125. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  126. };
  127. static struct resource codec_resources[] = {
  128. /* Headset microphone insertion or removal */
  129. {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
  130. /* Hook-switch press or release */
  131. {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
  132. /* Headset insertion or removal */
  133. {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
  134. /* Audio short */
  135. {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
  136. };
  137. static struct resource battery_resources[] = {
  138. {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
  139. {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
  140. };
  141. static struct resource charger_resources[] = {
  142. {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
  143. {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,},
  144. {PM8607_IRQ_CHG_FAIL, PM8607_IRQ_CHG_FAIL, "charging timeout", IORESOURCE_IRQ,},
  145. {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging fault", IORESOURCE_IRQ,},
  146. {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,},
  147. {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
  148. {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
  149. };
  150. static struct resource rtc_resources[] = {
  151. {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
  152. };
  153. static struct mfd_cell bk_devs[] = {
  154. {
  155. .name = "88pm860x-backlight",
  156. .id = 0,
  157. .num_resources = ARRAY_SIZE(bk0_resources),
  158. .resources = bk0_resources,
  159. }, {
  160. .name = "88pm860x-backlight",
  161. .id = 1,
  162. .num_resources = ARRAY_SIZE(bk1_resources),
  163. .resources = bk1_resources,
  164. }, {
  165. .name = "88pm860x-backlight",
  166. .id = 2,
  167. .num_resources = ARRAY_SIZE(bk2_resources),
  168. .resources = bk2_resources,
  169. },
  170. };
  171. static struct mfd_cell led_devs[] = {
  172. {
  173. .name = "88pm860x-led",
  174. .id = 0,
  175. .num_resources = ARRAY_SIZE(led0_resources),
  176. .resources = led0_resources,
  177. }, {
  178. .name = "88pm860x-led",
  179. .id = 1,
  180. .num_resources = ARRAY_SIZE(led1_resources),
  181. .resources = led1_resources,
  182. }, {
  183. .name = "88pm860x-led",
  184. .id = 2,
  185. .num_resources = ARRAY_SIZE(led2_resources),
  186. .resources = led2_resources,
  187. }, {
  188. .name = "88pm860x-led",
  189. .id = 3,
  190. .num_resources = ARRAY_SIZE(led3_resources),
  191. .resources = led3_resources,
  192. }, {
  193. .name = "88pm860x-led",
  194. .id = 4,
  195. .num_resources = ARRAY_SIZE(led4_resources),
  196. .resources = led4_resources,
  197. }, {
  198. .name = "88pm860x-led",
  199. .id = 5,
  200. .num_resources = ARRAY_SIZE(led5_resources),
  201. .resources = led5_resources,
  202. },
  203. };
  204. static struct mfd_cell reg_devs[] = {
  205. {
  206. .name = "88pm860x-regulator",
  207. .id = 0,
  208. .num_resources = ARRAY_SIZE(buck1_resources),
  209. .resources = buck1_resources,
  210. }, {
  211. .name = "88pm860x-regulator",
  212. .id = 1,
  213. .num_resources = ARRAY_SIZE(buck2_resources),
  214. .resources = buck2_resources,
  215. }, {
  216. .name = "88pm860x-regulator",
  217. .id = 2,
  218. .num_resources = ARRAY_SIZE(buck3_resources),
  219. .resources = buck3_resources,
  220. }, {
  221. .name = "88pm860x-regulator",
  222. .id = 3,
  223. .num_resources = ARRAY_SIZE(ldo1_resources),
  224. .resources = ldo1_resources,
  225. }, {
  226. .name = "88pm860x-regulator",
  227. .id = 4,
  228. .num_resources = ARRAY_SIZE(ldo2_resources),
  229. .resources = ldo2_resources,
  230. }, {
  231. .name = "88pm860x-regulator",
  232. .id = 5,
  233. .num_resources = ARRAY_SIZE(ldo3_resources),
  234. .resources = ldo3_resources,
  235. }, {
  236. .name = "88pm860x-regulator",
  237. .id = 6,
  238. .num_resources = ARRAY_SIZE(ldo4_resources),
  239. .resources = ldo4_resources,
  240. }, {
  241. .name = "88pm860x-regulator",
  242. .id = 7,
  243. .num_resources = ARRAY_SIZE(ldo5_resources),
  244. .resources = ldo5_resources,
  245. }, {
  246. .name = "88pm860x-regulator",
  247. .id = 8,
  248. .num_resources = ARRAY_SIZE(ldo6_resources),
  249. .resources = ldo6_resources,
  250. }, {
  251. .name = "88pm860x-regulator",
  252. .id = 9,
  253. .num_resources = ARRAY_SIZE(ldo7_resources),
  254. .resources = ldo7_resources,
  255. }, {
  256. .name = "88pm860x-regulator",
  257. .id = 10,
  258. .num_resources = ARRAY_SIZE(ldo8_resources),
  259. .resources = ldo8_resources,
  260. }, {
  261. .name = "88pm860x-regulator",
  262. .id = 11,
  263. .num_resources = ARRAY_SIZE(ldo9_resources),
  264. .resources = ldo9_resources,
  265. }, {
  266. .name = "88pm860x-regulator",
  267. .id = 12,
  268. .num_resources = ARRAY_SIZE(ldo10_resources),
  269. .resources = ldo10_resources,
  270. }, {
  271. .name = "88pm860x-regulator",
  272. .id = 13,
  273. .num_resources = ARRAY_SIZE(ldo12_resources),
  274. .resources = ldo12_resources,
  275. }, {
  276. .name = "88pm860x-regulator",
  277. .id = 14,
  278. .num_resources = ARRAY_SIZE(ldo_vibrator_resources),
  279. .resources = ldo_vibrator_resources,
  280. }, {
  281. .name = "88pm860x-regulator",
  282. .id = 15,
  283. .num_resources = ARRAY_SIZE(ldo14_resources),
  284. .resources = ldo14_resources,
  285. },
  286. };
  287. static struct mfd_cell touch_devs[] = {
  288. {"88pm860x-touch", -1,},
  289. };
  290. static struct mfd_cell onkey_devs[] = {
  291. {"88pm860x-onkey", -1,},
  292. };
  293. static struct mfd_cell codec_devs[] = {
  294. {"88pm860x-codec", -1,},
  295. };
  296. static struct regulator_consumer_supply preg_supply[] = {
  297. REGULATOR_SUPPLY("preg", "charger-manager"),
  298. };
  299. static struct regulator_init_data preg_init_data = {
  300. .num_consumer_supplies = ARRAY_SIZE(preg_supply),
  301. .consumer_supplies = &preg_supply[0],
  302. };
  303. static struct charger_regulator chg_desc_regulator_data[] = {
  304. { .regulator_name = "preg", },
  305. };
  306. static struct mfd_cell power_devs[] = {
  307. {"88pm860x-battery", -1,},
  308. {"88pm860x-charger", -1,},
  309. {"88pm860x-preg", -1,},
  310. {"charger-manager", -1,},
  311. };
  312. static struct mfd_cell rtc_devs[] = {
  313. {"88pm860x-rtc", -1,},
  314. };
  315. struct pm860x_irq_data {
  316. int reg;
  317. int mask_reg;
  318. int enable; /* enable or not */
  319. int offs; /* bit offset in mask register */
  320. };
  321. static struct pm860x_irq_data pm860x_irqs[] = {
  322. [PM8607_IRQ_ONKEY] = {
  323. .reg = PM8607_INT_STATUS1,
  324. .mask_reg = PM8607_INT_MASK_1,
  325. .offs = 1 << 0,
  326. },
  327. [PM8607_IRQ_EXTON] = {
  328. .reg = PM8607_INT_STATUS1,
  329. .mask_reg = PM8607_INT_MASK_1,
  330. .offs = 1 << 1,
  331. },
  332. [PM8607_IRQ_CHG] = {
  333. .reg = PM8607_INT_STATUS1,
  334. .mask_reg = PM8607_INT_MASK_1,
  335. .offs = 1 << 2,
  336. },
  337. [PM8607_IRQ_BAT] = {
  338. .reg = PM8607_INT_STATUS1,
  339. .mask_reg = PM8607_INT_MASK_1,
  340. .offs = 1 << 3,
  341. },
  342. [PM8607_IRQ_RTC] = {
  343. .reg = PM8607_INT_STATUS1,
  344. .mask_reg = PM8607_INT_MASK_1,
  345. .offs = 1 << 4,
  346. },
  347. [PM8607_IRQ_CC] = {
  348. .reg = PM8607_INT_STATUS1,
  349. .mask_reg = PM8607_INT_MASK_1,
  350. .offs = 1 << 5,
  351. },
  352. [PM8607_IRQ_VBAT] = {
  353. .reg = PM8607_INT_STATUS2,
  354. .mask_reg = PM8607_INT_MASK_2,
  355. .offs = 1 << 0,
  356. },
  357. [PM8607_IRQ_VCHG] = {
  358. .reg = PM8607_INT_STATUS2,
  359. .mask_reg = PM8607_INT_MASK_2,
  360. .offs = 1 << 1,
  361. },
  362. [PM8607_IRQ_VSYS] = {
  363. .reg = PM8607_INT_STATUS2,
  364. .mask_reg = PM8607_INT_MASK_2,
  365. .offs = 1 << 2,
  366. },
  367. [PM8607_IRQ_TINT] = {
  368. .reg = PM8607_INT_STATUS2,
  369. .mask_reg = PM8607_INT_MASK_2,
  370. .offs = 1 << 3,
  371. },
  372. [PM8607_IRQ_GPADC0] = {
  373. .reg = PM8607_INT_STATUS2,
  374. .mask_reg = PM8607_INT_MASK_2,
  375. .offs = 1 << 4,
  376. },
  377. [PM8607_IRQ_GPADC1] = {
  378. .reg = PM8607_INT_STATUS2,
  379. .mask_reg = PM8607_INT_MASK_2,
  380. .offs = 1 << 5,
  381. },
  382. [PM8607_IRQ_GPADC2] = {
  383. .reg = PM8607_INT_STATUS2,
  384. .mask_reg = PM8607_INT_MASK_2,
  385. .offs = 1 << 6,
  386. },
  387. [PM8607_IRQ_GPADC3] = {
  388. .reg = PM8607_INT_STATUS2,
  389. .mask_reg = PM8607_INT_MASK_2,
  390. .offs = 1 << 7,
  391. },
  392. [PM8607_IRQ_AUDIO_SHORT] = {
  393. .reg = PM8607_INT_STATUS3,
  394. .mask_reg = PM8607_INT_MASK_3,
  395. .offs = 1 << 0,
  396. },
  397. [PM8607_IRQ_PEN] = {
  398. .reg = PM8607_INT_STATUS3,
  399. .mask_reg = PM8607_INT_MASK_3,
  400. .offs = 1 << 1,
  401. },
  402. [PM8607_IRQ_HEADSET] = {
  403. .reg = PM8607_INT_STATUS3,
  404. .mask_reg = PM8607_INT_MASK_3,
  405. .offs = 1 << 2,
  406. },
  407. [PM8607_IRQ_HOOK] = {
  408. .reg = PM8607_INT_STATUS3,
  409. .mask_reg = PM8607_INT_MASK_3,
  410. .offs = 1 << 3,
  411. },
  412. [PM8607_IRQ_MICIN] = {
  413. .reg = PM8607_INT_STATUS3,
  414. .mask_reg = PM8607_INT_MASK_3,
  415. .offs = 1 << 4,
  416. },
  417. [PM8607_IRQ_CHG_FAIL] = {
  418. .reg = PM8607_INT_STATUS3,
  419. .mask_reg = PM8607_INT_MASK_3,
  420. .offs = 1 << 5,
  421. },
  422. [PM8607_IRQ_CHG_DONE] = {
  423. .reg = PM8607_INT_STATUS3,
  424. .mask_reg = PM8607_INT_MASK_3,
  425. .offs = 1 << 6,
  426. },
  427. [PM8607_IRQ_CHG_FAULT] = {
  428. .reg = PM8607_INT_STATUS3,
  429. .mask_reg = PM8607_INT_MASK_3,
  430. .offs = 1 << 7,
  431. },
  432. };
  433. static irqreturn_t pm860x_irq(int irq, void *data)
  434. {
  435. struct pm860x_chip *chip = data;
  436. struct pm860x_irq_data *irq_data;
  437. struct i2c_client *i2c;
  438. int read_reg = -1, value = 0;
  439. int i;
  440. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  441. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  442. irq_data = &pm860x_irqs[i];
  443. if (read_reg != irq_data->reg) {
  444. read_reg = irq_data->reg;
  445. value = pm860x_reg_read(i2c, irq_data->reg);
  446. }
  447. if (value & irq_data->enable)
  448. handle_nested_irq(chip->irq_base + i);
  449. }
  450. return IRQ_HANDLED;
  451. }
  452. static void pm860x_irq_lock(struct irq_data *data)
  453. {
  454. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  455. mutex_lock(&chip->irq_lock);
  456. }
  457. static void pm860x_irq_sync_unlock(struct irq_data *data)
  458. {
  459. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  460. struct pm860x_irq_data *irq_data;
  461. struct i2c_client *i2c;
  462. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  463. unsigned char mask[3];
  464. int i;
  465. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  466. /* Load cached value. In initial, all IRQs are masked */
  467. for (i = 0; i < 3; i++)
  468. mask[i] = cached[i];
  469. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  470. irq_data = &pm860x_irqs[i];
  471. switch (irq_data->mask_reg) {
  472. case PM8607_INT_MASK_1:
  473. mask[0] &= ~irq_data->offs;
  474. mask[0] |= irq_data->enable;
  475. break;
  476. case PM8607_INT_MASK_2:
  477. mask[1] &= ~irq_data->offs;
  478. mask[1] |= irq_data->enable;
  479. break;
  480. case PM8607_INT_MASK_3:
  481. mask[2] &= ~irq_data->offs;
  482. mask[2] |= irq_data->enable;
  483. break;
  484. default:
  485. dev_err(chip->dev, "wrong IRQ\n");
  486. break;
  487. }
  488. }
  489. /* update mask into registers */
  490. for (i = 0; i < 3; i++) {
  491. if (mask[i] != cached[i]) {
  492. cached[i] = mask[i];
  493. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  494. }
  495. }
  496. mutex_unlock(&chip->irq_lock);
  497. }
  498. static void pm860x_irq_enable(struct irq_data *data)
  499. {
  500. pm860x_irqs[data->hwirq].enable = pm860x_irqs[data->hwirq].offs;
  501. }
  502. static void pm860x_irq_disable(struct irq_data *data)
  503. {
  504. pm860x_irqs[data->hwirq].enable = 0;
  505. }
  506. static struct irq_chip pm860x_irq_chip = {
  507. .name = "88pm860x",
  508. .irq_bus_lock = pm860x_irq_lock,
  509. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  510. .irq_enable = pm860x_irq_enable,
  511. .irq_disable = pm860x_irq_disable,
  512. };
  513. static int pm860x_irq_domain_map(struct irq_domain *d, unsigned int virq,
  514. irq_hw_number_t hw)
  515. {
  516. irq_set_chip_data(virq, d->host_data);
  517. irq_set_chip_and_handler(virq, &pm860x_irq_chip, handle_edge_irq);
  518. irq_set_nested_thread(virq, 1);
  519. #ifdef CONFIG_ARM
  520. set_irq_flags(virq, IRQF_VALID);
  521. #else
  522. irq_set_noprobe(virq);
  523. #endif
  524. return 0;
  525. }
  526. static struct irq_domain_ops pm860x_irq_domain_ops = {
  527. .map = pm860x_irq_domain_map,
  528. .xlate = irq_domain_xlate_onetwocell,
  529. };
  530. static int device_irq_init(struct pm860x_chip *chip,
  531. struct pm860x_platform_data *pdata)
  532. {
  533. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  534. : chip->companion;
  535. unsigned char status_buf[INT_STATUS_NUM];
  536. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  537. int data, mask, ret = -EINVAL;
  538. int nr_irqs, irq_base = -1;
  539. struct device_node *node = i2c->dev.of_node;
  540. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  541. | PM8607_B0_MISC1_INT_MASK;
  542. data = 0;
  543. chip->irq_mode = 0;
  544. if (pdata && pdata->irq_mode) {
  545. /*
  546. * irq_mode defines the way of clearing interrupt. If it's 1,
  547. * clear IRQ by write. Otherwise, clear it by read.
  548. * This control bit is valid from 88PM8607 B0 steping.
  549. */
  550. data |= PM8607_B0_MISC1_INT_CLEAR;
  551. chip->irq_mode = 1;
  552. }
  553. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  554. if (ret < 0)
  555. goto out;
  556. /* mask all IRQs */
  557. memset(status_buf, 0, INT_STATUS_NUM);
  558. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  559. INT_STATUS_NUM, status_buf);
  560. if (ret < 0)
  561. goto out;
  562. if (chip->irq_mode) {
  563. /* clear interrupt status by write */
  564. memset(status_buf, 0xFF, INT_STATUS_NUM);
  565. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  566. INT_STATUS_NUM, status_buf);
  567. } else {
  568. /* clear interrupt status by read */
  569. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  570. INT_STATUS_NUM, status_buf);
  571. }
  572. if (ret < 0)
  573. goto out;
  574. mutex_init(&chip->irq_lock);
  575. if (pdata && pdata->irq_base)
  576. irq_base = pdata->irq_base;
  577. nr_irqs = ARRAY_SIZE(pm860x_irqs);
  578. chip->irq_base = irq_alloc_descs(irq_base, 0, nr_irqs, 0);
  579. if (chip->irq_base < 0) {
  580. dev_err(&i2c->dev, "Failed to allocate interrupts, ret:%d\n",
  581. chip->irq_base);
  582. ret = -EBUSY;
  583. goto out;
  584. }
  585. irq_domain_add_legacy(node, nr_irqs, chip->irq_base, 0,
  586. &pm860x_irq_domain_ops, chip);
  587. chip->core_irq = i2c->irq;
  588. if (!chip->core_irq)
  589. goto out;
  590. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags | IRQF_ONESHOT,
  591. "88pm860x", chip);
  592. if (ret) {
  593. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  594. chip->core_irq = 0;
  595. }
  596. return 0;
  597. out:
  598. chip->core_irq = 0;
  599. return ret;
  600. }
  601. static void device_irq_exit(struct pm860x_chip *chip)
  602. {
  603. if (chip->core_irq)
  604. free_irq(chip->core_irq, chip);
  605. }
  606. int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
  607. {
  608. int ret = -EIO;
  609. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  610. chip->client : chip->companion;
  611. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  612. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  613. __func__, chip->osc_vote,
  614. chip->osc_status);
  615. mutex_lock(&chip->osc_lock);
  616. /* Update voting status */
  617. chip->osc_vote |= client;
  618. /* If reference group is off - turn on*/
  619. if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
  620. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  621. /* Enable Reference group Vsys */
  622. if (pm860x_set_bits(i2c, PM8606_VSYS,
  623. PM8606_VSYS_EN, PM8606_VSYS_EN))
  624. goto out;
  625. /*Enable Internal Oscillator */
  626. if (pm860x_set_bits(i2c, PM8606_MISC,
  627. PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
  628. goto out;
  629. /* Update status (only if writes succeed) */
  630. chip->osc_status = PM8606_REF_GP_OSC_ON;
  631. }
  632. mutex_unlock(&chip->osc_lock);
  633. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  634. __func__, chip->osc_vote,
  635. chip->osc_status, ret);
  636. return 0;
  637. out:
  638. mutex_unlock(&chip->osc_lock);
  639. return ret;
  640. }
  641. EXPORT_SYMBOL(pm8606_osc_enable);
  642. int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
  643. {
  644. int ret = -EIO;
  645. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  646. chip->client : chip->companion;
  647. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  648. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  649. __func__, chip->osc_vote,
  650. chip->osc_status);
  651. mutex_lock(&chip->osc_lock);
  652. /*Update voting status */
  653. chip->osc_vote &= ~(client);
  654. /* If reference group is off and this is the last client to release
  655. * - turn off */
  656. if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
  657. (chip->osc_vote == REF_GP_NO_CLIENTS)) {
  658. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  659. /* Disable Reference group Vsys */
  660. if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
  661. goto out;
  662. /* Disable Internal Oscillator */
  663. if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
  664. goto out;
  665. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  666. }
  667. mutex_unlock(&chip->osc_lock);
  668. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  669. __func__, chip->osc_vote,
  670. chip->osc_status, ret);
  671. return 0;
  672. out:
  673. mutex_unlock(&chip->osc_lock);
  674. return ret;
  675. }
  676. EXPORT_SYMBOL(pm8606_osc_disable);
  677. static void device_osc_init(struct i2c_client *i2c)
  678. {
  679. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  680. mutex_init(&chip->osc_lock);
  681. /* init portofino reference group voting and status */
  682. /* Disable Reference group Vsys */
  683. pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
  684. /* Disable Internal Oscillator */
  685. pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
  686. chip->osc_vote = REF_GP_NO_CLIENTS;
  687. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  688. }
  689. static void device_bk_init(struct pm860x_chip *chip,
  690. struct pm860x_platform_data *pdata)
  691. {
  692. int ret, i;
  693. if (pdata && pdata->backlight) {
  694. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  695. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  696. for (i = 0; i < pdata->num_backlights; i++) {
  697. bk_devs[i].platform_data = &pdata->backlight[i];
  698. bk_devs[i].pdata_size =
  699. sizeof(struct pm860x_backlight_pdata);
  700. }
  701. }
  702. ret = mfd_add_devices(chip->dev, 0, bk_devs,
  703. ARRAY_SIZE(bk_devs), NULL, 0, NULL);
  704. if (ret < 0)
  705. dev_err(chip->dev, "Failed to add backlight subdev\n");
  706. }
  707. static void device_led_init(struct pm860x_chip *chip,
  708. struct pm860x_platform_data *pdata)
  709. {
  710. int ret, i;
  711. if (pdata && pdata->led) {
  712. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  713. pdata->num_leds = ARRAY_SIZE(led_devs);
  714. for (i = 0; i < pdata->num_leds; i++) {
  715. led_devs[i].platform_data = &pdata->led[i];
  716. led_devs[i].pdata_size =
  717. sizeof(struct pm860x_led_pdata);
  718. }
  719. }
  720. ret = mfd_add_devices(chip->dev, 0, led_devs,
  721. ARRAY_SIZE(led_devs), NULL, 0, NULL);
  722. if (ret < 0) {
  723. dev_err(chip->dev, "Failed to add led subdev\n");
  724. return;
  725. }
  726. }
  727. static void device_regulator_init(struct pm860x_chip *chip,
  728. struct pm860x_platform_data *pdata)
  729. {
  730. int ret;
  731. if (pdata == NULL)
  732. return;
  733. if (pdata->buck1) {
  734. reg_devs[0].platform_data = pdata->buck1;
  735. reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
  736. }
  737. if (pdata->buck2) {
  738. reg_devs[1].platform_data = pdata->buck2;
  739. reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
  740. }
  741. if (pdata->buck3) {
  742. reg_devs[2].platform_data = pdata->buck3;
  743. reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
  744. }
  745. if (pdata->ldo1) {
  746. reg_devs[3].platform_data = pdata->ldo1;
  747. reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
  748. }
  749. if (pdata->ldo2) {
  750. reg_devs[4].platform_data = pdata->ldo2;
  751. reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
  752. }
  753. if (pdata->ldo3) {
  754. reg_devs[5].platform_data = pdata->ldo3;
  755. reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
  756. }
  757. if (pdata->ldo4) {
  758. reg_devs[6].platform_data = pdata->ldo4;
  759. reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
  760. }
  761. if (pdata->ldo5) {
  762. reg_devs[7].platform_data = pdata->ldo5;
  763. reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
  764. }
  765. if (pdata->ldo6) {
  766. reg_devs[8].platform_data = pdata->ldo6;
  767. reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
  768. }
  769. if (pdata->ldo7) {
  770. reg_devs[9].platform_data = pdata->ldo7;
  771. reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
  772. }
  773. if (pdata->ldo8) {
  774. reg_devs[10].platform_data = pdata->ldo8;
  775. reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
  776. }
  777. if (pdata->ldo9) {
  778. reg_devs[11].platform_data = pdata->ldo9;
  779. reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
  780. }
  781. if (pdata->ldo10) {
  782. reg_devs[12].platform_data = pdata->ldo10;
  783. reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
  784. }
  785. if (pdata->ldo12) {
  786. reg_devs[13].platform_data = pdata->ldo12;
  787. reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
  788. }
  789. if (pdata->ldo_vibrator) {
  790. reg_devs[14].platform_data = pdata->ldo_vibrator;
  791. reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
  792. }
  793. if (pdata->ldo14) {
  794. reg_devs[15].platform_data = pdata->ldo14;
  795. reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
  796. }
  797. ret = mfd_add_devices(chip->dev, 0, reg_devs,
  798. ARRAY_SIZE(reg_devs), NULL, 0, NULL);
  799. if (ret < 0) {
  800. dev_err(chip->dev, "Failed to add regulator subdev\n");
  801. return;
  802. }
  803. }
  804. static void device_rtc_init(struct pm860x_chip *chip,
  805. struct pm860x_platform_data *pdata)
  806. {
  807. int ret;
  808. if ((pdata == NULL))
  809. return;
  810. rtc_devs[0].platform_data = pdata->rtc;
  811. rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
  812. rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
  813. rtc_devs[0].resources = &rtc_resources[0];
  814. ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
  815. ARRAY_SIZE(rtc_devs), &rtc_resources[0],
  816. chip->irq_base, NULL);
  817. if (ret < 0)
  818. dev_err(chip->dev, "Failed to add rtc subdev\n");
  819. }
  820. static void device_touch_init(struct pm860x_chip *chip,
  821. struct pm860x_platform_data *pdata)
  822. {
  823. int ret;
  824. if (pdata == NULL)
  825. return;
  826. touch_devs[0].platform_data = pdata->touch;
  827. touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
  828. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  829. touch_devs[0].resources = &touch_resources[0];
  830. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  831. ARRAY_SIZE(touch_devs), &touch_resources[0],
  832. chip->irq_base, NULL);
  833. if (ret < 0)
  834. dev_err(chip->dev, "Failed to add touch subdev\n");
  835. }
  836. static void device_power_init(struct pm860x_chip *chip,
  837. struct pm860x_platform_data *pdata)
  838. {
  839. int ret;
  840. if (pdata == NULL)
  841. return;
  842. power_devs[0].platform_data = pdata->power;
  843. power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
  844. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  845. power_devs[0].resources = &battery_resources[0],
  846. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  847. &battery_resources[0], chip->irq_base, NULL);
  848. if (ret < 0)
  849. dev_err(chip->dev, "Failed to add battery subdev\n");
  850. power_devs[1].platform_data = pdata->power;
  851. power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
  852. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  853. power_devs[1].resources = &charger_resources[0],
  854. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  855. &charger_resources[0], chip->irq_base, NULL);
  856. if (ret < 0)
  857. dev_err(chip->dev, "Failed to add charger subdev\n");
  858. power_devs[2].platform_data = &preg_init_data;
  859. power_devs[2].pdata_size = sizeof(struct regulator_init_data);
  860. ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
  861. NULL, chip->irq_base, NULL);
  862. if (ret < 0)
  863. dev_err(chip->dev, "Failed to add preg subdev\n");
  864. if (pdata->chg_desc) {
  865. pdata->chg_desc->charger_regulators =
  866. &chg_desc_regulator_data[0];
  867. pdata->chg_desc->num_charger_regulators =
  868. ARRAY_SIZE(chg_desc_regulator_data),
  869. power_devs[3].platform_data = pdata->chg_desc;
  870. power_devs[3].pdata_size = sizeof(*pdata->chg_desc);
  871. ret = mfd_add_devices(chip->dev, 0, &power_devs[3], 1,
  872. NULL, chip->irq_base, NULL);
  873. if (ret < 0)
  874. dev_err(chip->dev, "Failed to add chg-manager subdev\n");
  875. }
  876. }
  877. static void device_onkey_init(struct pm860x_chip *chip,
  878. struct pm860x_platform_data *pdata)
  879. {
  880. int ret;
  881. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  882. onkey_devs[0].resources = &onkey_resources[0],
  883. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  884. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  885. chip->irq_base, NULL);
  886. if (ret < 0)
  887. dev_err(chip->dev, "Failed to add onkey subdev\n");
  888. }
  889. static void device_codec_init(struct pm860x_chip *chip,
  890. struct pm860x_platform_data *pdata)
  891. {
  892. int ret;
  893. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  894. codec_devs[0].resources = &codec_resources[0],
  895. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  896. ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
  897. NULL);
  898. if (ret < 0)
  899. dev_err(chip->dev, "Failed to add codec subdev\n");
  900. }
  901. static void device_8607_init(struct pm860x_chip *chip,
  902. struct i2c_client *i2c,
  903. struct pm860x_platform_data *pdata)
  904. {
  905. int data, ret;
  906. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  907. if (ret < 0) {
  908. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  909. goto out;
  910. }
  911. switch (ret & PM8607_VERSION_MASK) {
  912. case 0x40:
  913. case 0x50:
  914. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  915. ret);
  916. break;
  917. default:
  918. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  919. "Chip ID: %02x\n", ret);
  920. goto out;
  921. }
  922. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  923. if (ret < 0) {
  924. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  925. goto out;
  926. }
  927. if (ret & PM8607_BUCK3_DOUBLE)
  928. chip->buck3_double = 1;
  929. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  930. if (ret < 0) {
  931. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  932. goto out;
  933. }
  934. if (pdata && (pdata->i2c_port == PI2C_PORT))
  935. data = PM8607_B0_MISC1_PI2C;
  936. else
  937. data = 0;
  938. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  939. if (ret < 0) {
  940. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  941. goto out;
  942. }
  943. ret = device_irq_init(chip, pdata);
  944. if (ret < 0)
  945. goto out;
  946. device_regulator_init(chip, pdata);
  947. device_rtc_init(chip, pdata);
  948. device_onkey_init(chip, pdata);
  949. device_touch_init(chip, pdata);
  950. device_power_init(chip, pdata);
  951. device_codec_init(chip, pdata);
  952. out:
  953. return;
  954. }
  955. static void device_8606_init(struct pm860x_chip *chip,
  956. struct i2c_client *i2c,
  957. struct pm860x_platform_data *pdata)
  958. {
  959. device_osc_init(i2c);
  960. device_bk_init(chip, pdata);
  961. device_led_init(chip, pdata);
  962. }
  963. static int pm860x_device_init(struct pm860x_chip *chip,
  964. struct pm860x_platform_data *pdata)
  965. {
  966. chip->core_irq = 0;
  967. switch (chip->id) {
  968. case CHIP_PM8606:
  969. device_8606_init(chip, chip->client, pdata);
  970. break;
  971. case CHIP_PM8607:
  972. device_8607_init(chip, chip->client, pdata);
  973. break;
  974. }
  975. if (chip->companion) {
  976. switch (chip->id) {
  977. case CHIP_PM8607:
  978. device_8606_init(chip, chip->companion, pdata);
  979. break;
  980. case CHIP_PM8606:
  981. device_8607_init(chip, chip->companion, pdata);
  982. break;
  983. }
  984. }
  985. return 0;
  986. }
  987. static void pm860x_device_exit(struct pm860x_chip *chip)
  988. {
  989. device_irq_exit(chip);
  990. mfd_remove_devices(chip->dev);
  991. }
  992. static int verify_addr(struct i2c_client *i2c)
  993. {
  994. unsigned short addr_8607[] = {0x30, 0x34};
  995. unsigned short addr_8606[] = {0x10, 0x11};
  996. int size, i;
  997. if (i2c == NULL)
  998. return 0;
  999. size = ARRAY_SIZE(addr_8606);
  1000. for (i = 0; i < size; i++) {
  1001. if (i2c->addr == *(addr_8606 + i))
  1002. return CHIP_PM8606;
  1003. }
  1004. size = ARRAY_SIZE(addr_8607);
  1005. for (i = 0; i < size; i++) {
  1006. if (i2c->addr == *(addr_8607 + i))
  1007. return CHIP_PM8607;
  1008. }
  1009. return 0;
  1010. }
  1011. static struct regmap_config pm860x_regmap_config = {
  1012. .reg_bits = 8,
  1013. .val_bits = 8,
  1014. };
  1015. static int pm860x_dt_init(struct device_node *np,
  1016. struct device *dev,
  1017. struct pm860x_platform_data *pdata)
  1018. {
  1019. int ret;
  1020. if (of_get_property(np, "marvell,88pm860x-irq-read-clr", NULL))
  1021. pdata->irq_mode = 1;
  1022. ret = of_property_read_u32(np, "marvell,88pm860x-slave-addr",
  1023. &pdata->companion_addr);
  1024. if (ret) {
  1025. dev_err(dev, "Not found \"marvell,88pm860x-slave-addr\" "
  1026. "property\n");
  1027. pdata->companion_addr = 0;
  1028. }
  1029. return 0;
  1030. }
  1031. static int pm860x_probe(struct i2c_client *client,
  1032. const struct i2c_device_id *id)
  1033. {
  1034. struct pm860x_platform_data *pdata = client->dev.platform_data;
  1035. struct device_node *node = client->dev.of_node;
  1036. struct pm860x_chip *chip;
  1037. int ret;
  1038. if (node && !pdata) {
  1039. /* parse DT to get platform data */
  1040. pdata = devm_kzalloc(&client->dev,
  1041. sizeof(struct pm860x_platform_data),
  1042. GFP_KERNEL);
  1043. if (!pdata)
  1044. return -ENOMEM;
  1045. ret = pm860x_dt_init(node, &client->dev, pdata);
  1046. if (ret)
  1047. goto err;
  1048. } else if (!pdata) {
  1049. pr_info("No platform data in %s!\n", __func__);
  1050. return -EINVAL;
  1051. }
  1052. chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL);
  1053. if (chip == NULL) {
  1054. ret = -ENOMEM;
  1055. goto err;
  1056. }
  1057. chip->id = verify_addr(client);
  1058. chip->regmap = regmap_init_i2c(client, &pm860x_regmap_config);
  1059. if (IS_ERR(chip->regmap)) {
  1060. ret = PTR_ERR(chip->regmap);
  1061. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  1062. ret);
  1063. kfree(chip);
  1064. return ret;
  1065. }
  1066. chip->client = client;
  1067. i2c_set_clientdata(client, chip);
  1068. chip->dev = &client->dev;
  1069. dev_set_drvdata(chip->dev, chip);
  1070. /*
  1071. * Both client and companion client shares same platform driver.
  1072. * Driver distinguishes them by pdata->companion_addr.
  1073. * pdata->companion_addr is only assigned if companion chip exists.
  1074. * At the same time, the companion_addr shouldn't equal to client
  1075. * address.
  1076. */
  1077. if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
  1078. chip->companion_addr = pdata->companion_addr;
  1079. chip->companion = i2c_new_dummy(chip->client->adapter,
  1080. chip->companion_addr);
  1081. chip->regmap_companion = regmap_init_i2c(chip->companion,
  1082. &pm860x_regmap_config);
  1083. if (IS_ERR(chip->regmap_companion)) {
  1084. ret = PTR_ERR(chip->regmap_companion);
  1085. dev_err(&chip->companion->dev,
  1086. "Failed to allocate register map: %d\n", ret);
  1087. return ret;
  1088. }
  1089. i2c_set_clientdata(chip->companion, chip);
  1090. }
  1091. pm860x_device_init(chip, pdata);
  1092. return 0;
  1093. err:
  1094. if (node)
  1095. devm_kfree(&client->dev, pdata);
  1096. return ret;
  1097. }
  1098. static int pm860x_remove(struct i2c_client *client)
  1099. {
  1100. struct pm860x_chip *chip = i2c_get_clientdata(client);
  1101. pm860x_device_exit(chip);
  1102. if (chip->companion) {
  1103. regmap_exit(chip->regmap_companion);
  1104. i2c_unregister_device(chip->companion);
  1105. }
  1106. regmap_exit(chip->regmap);
  1107. kfree(chip);
  1108. return 0;
  1109. }
  1110. #ifdef CONFIG_PM_SLEEP
  1111. static int pm860x_suspend(struct device *dev)
  1112. {
  1113. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1114. struct pm860x_chip *chip = i2c_get_clientdata(client);
  1115. if (device_may_wakeup(dev) && chip->wakeup_flag)
  1116. enable_irq_wake(chip->core_irq);
  1117. return 0;
  1118. }
  1119. static int pm860x_resume(struct device *dev)
  1120. {
  1121. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1122. struct pm860x_chip *chip = i2c_get_clientdata(client);
  1123. if (device_may_wakeup(dev) && chip->wakeup_flag)
  1124. disable_irq_wake(chip->core_irq);
  1125. return 0;
  1126. }
  1127. #endif
  1128. static SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
  1129. static const struct i2c_device_id pm860x_id_table[] = {
  1130. { "88PM860x", 0 },
  1131. {}
  1132. };
  1133. MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
  1134. static const struct of_device_id pm860x_dt_ids[] = {
  1135. { .compatible = "marvell,88pm860x", },
  1136. {},
  1137. };
  1138. MODULE_DEVICE_TABLE(of, pm860x_dt_ids);
  1139. static struct i2c_driver pm860x_driver = {
  1140. .driver = {
  1141. .name = "88PM860x",
  1142. .owner = THIS_MODULE,
  1143. .pm = &pm860x_pm_ops,
  1144. .of_match_table = of_match_ptr(pm860x_dt_ids),
  1145. },
  1146. .probe = pm860x_probe,
  1147. .remove = pm860x_remove,
  1148. .id_table = pm860x_id_table,
  1149. };
  1150. static int __init pm860x_i2c_init(void)
  1151. {
  1152. int ret;
  1153. ret = i2c_add_driver(&pm860x_driver);
  1154. if (ret != 0)
  1155. pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
  1156. return ret;
  1157. }
  1158. subsys_initcall(pm860x_i2c_init);
  1159. static void __exit pm860x_i2c_exit(void)
  1160. {
  1161. i2c_del_driver(&pm860x_driver);
  1162. }
  1163. module_exit(pm860x_i2c_exit);
  1164. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  1165. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  1166. MODULE_LICENSE("GPL");