88pm860x-core.c 33 KB

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