88pm860x-core.c 34 KB

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