88pm860x-core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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/i2c.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/mfd/88pm860x.h>
  19. #include <linux/regulator/machine.h>
  20. #define INT_STATUS_NUM 3
  21. static struct resource bk0_resources[] __devinitdata = {
  22. {2, 2, "duty cycle", IORESOURCE_REG, },
  23. {3, 3, "always on", IORESOURCE_REG, },
  24. {3, 3, "current", IORESOURCE_REG, },
  25. };
  26. static struct resource bk1_resources[] __devinitdata = {
  27. {4, 4, "duty cycle", IORESOURCE_REG, },
  28. {5, 5, "always on", IORESOURCE_REG, },
  29. {5, 5, "current", IORESOURCE_REG, },
  30. };
  31. static struct resource bk2_resources[] __devinitdata = {
  32. {6, 6, "duty cycle", IORESOURCE_REG, },
  33. {7, 7, "always on", IORESOURCE_REG, },
  34. {5, 5, "current", IORESOURCE_REG, },
  35. };
  36. static struct resource led0_resources[] __devinitdata = {
  37. /* RGB1 Red LED */
  38. {0xd, 0xd, "control", IORESOURCE_REG, },
  39. {0xc, 0xc, "blink", IORESOURCE_REG, },
  40. };
  41. static struct resource led1_resources[] __devinitdata = {
  42. /* RGB1 Green LED */
  43. {0xe, 0xe, "control", IORESOURCE_REG, },
  44. {0xc, 0xc, "blink", IORESOURCE_REG, },
  45. };
  46. static struct resource led2_resources[] __devinitdata = {
  47. /* RGB1 Blue LED */
  48. {0xf, 0xf, "control", IORESOURCE_REG, },
  49. {0xc, 0xc, "blink", IORESOURCE_REG, },
  50. };
  51. static struct resource led3_resources[] __devinitdata = {
  52. /* RGB2 Red LED */
  53. {0x9, 0x9, "control", IORESOURCE_REG, },
  54. {0x8, 0x8, "blink", IORESOURCE_REG, },
  55. };
  56. static struct resource led4_resources[] __devinitdata = {
  57. /* RGB2 Green LED */
  58. {0xa, 0xa, "control", IORESOURCE_REG, },
  59. {0x8, 0x8, "blink", IORESOURCE_REG, },
  60. };
  61. static struct resource led5_resources[] __devinitdata = {
  62. /* RGB2 Blue LED */
  63. {0xb, 0xb, "control", IORESOURCE_REG, },
  64. {0x8, 0x8, "blink", IORESOURCE_REG, },
  65. };
  66. static struct resource regulator_resources[] __devinitdata = {
  67. {PM8607_ID_BUCK1, PM8607_ID_BUCK1, "buck-1", IORESOURCE_REG,},
  68. {PM8607_ID_BUCK2, PM8607_ID_BUCK2, "buck-2", IORESOURCE_REG,},
  69. {PM8607_ID_BUCK3, PM8607_ID_BUCK3, "buck-3", IORESOURCE_REG,},
  70. {PM8607_ID_LDO1, PM8607_ID_LDO1, "ldo-01", IORESOURCE_REG,},
  71. {PM8607_ID_LDO2, PM8607_ID_LDO2, "ldo-02", IORESOURCE_REG,},
  72. {PM8607_ID_LDO3, PM8607_ID_LDO3, "ldo-03", IORESOURCE_REG,},
  73. {PM8607_ID_LDO4, PM8607_ID_LDO4, "ldo-04", IORESOURCE_REG,},
  74. {PM8607_ID_LDO5, PM8607_ID_LDO5, "ldo-05", IORESOURCE_REG,},
  75. {PM8607_ID_LDO6, PM8607_ID_LDO6, "ldo-06", IORESOURCE_REG,},
  76. {PM8607_ID_LDO7, PM8607_ID_LDO7, "ldo-07", IORESOURCE_REG,},
  77. {PM8607_ID_LDO8, PM8607_ID_LDO8, "ldo-08", IORESOURCE_REG,},
  78. {PM8607_ID_LDO9, PM8607_ID_LDO9, "ldo-09", IORESOURCE_REG,},
  79. {PM8607_ID_LDO10, PM8607_ID_LDO10, "ldo-10", IORESOURCE_REG,},
  80. {PM8607_ID_LDO11, PM8607_ID_LDO11, "ldo-11", IORESOURCE_REG,},
  81. {PM8607_ID_LDO12, PM8607_ID_LDO12, "ldo-12", IORESOURCE_REG,},
  82. {PM8607_ID_LDO13, PM8607_ID_LDO13, "ldo-13", IORESOURCE_REG,},
  83. {PM8607_ID_LDO14, PM8607_ID_LDO14, "ldo-14", IORESOURCE_REG,},
  84. {PM8607_ID_LDO15, PM8607_ID_LDO15, "ldo-15", IORESOURCE_REG,},
  85. };
  86. static struct resource touch_resources[] __devinitdata = {
  87. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  88. };
  89. static struct resource onkey_resources[] __devinitdata = {
  90. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  91. };
  92. static struct resource codec_resources[] __devinitdata = {
  93. /* Headset microphone insertion or removal */
  94. {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
  95. /* Hook-switch press or release */
  96. {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
  97. /* Headset insertion or removal */
  98. {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
  99. /* Audio short */
  100. {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
  101. };
  102. static struct resource battery_resources[] __devinitdata = {
  103. {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
  104. {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
  105. };
  106. static struct resource charger_resources[] __devinitdata = {
  107. {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
  108. {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,},
  109. {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,},
  110. {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,},
  111. {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
  112. {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
  113. };
  114. static struct resource preg_resources[] __devinitdata = {
  115. {PM8606_ID_PREG, PM8606_ID_PREG, "preg", IORESOURCE_REG,},
  116. };
  117. static struct resource rtc_resources[] __devinitdata = {
  118. {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
  119. };
  120. static struct mfd_cell bk_devs[] = {
  121. {
  122. .name = "88pm860x-backlight",
  123. .id = 0,
  124. .num_resources = ARRAY_SIZE(bk0_resources),
  125. .resources = bk0_resources,
  126. }, {
  127. .name = "88pm860x-backlight",
  128. .id = 1,
  129. .num_resources = ARRAY_SIZE(bk1_resources),
  130. .resources = bk1_resources,
  131. }, {
  132. .name = "88pm860x-backlight",
  133. .id = 2,
  134. .num_resources = ARRAY_SIZE(bk2_resources),
  135. .resources = bk2_resources,
  136. },
  137. };
  138. static struct mfd_cell led_devs[] = {
  139. {
  140. .name = "88pm860x-led",
  141. .id = 0,
  142. .num_resources = ARRAY_SIZE(led0_resources),
  143. .resources = led0_resources,
  144. }, {
  145. .name = "88pm860x-led",
  146. .id = 1,
  147. .num_resources = ARRAY_SIZE(led1_resources),
  148. .resources = led1_resources,
  149. }, {
  150. .name = "88pm860x-led",
  151. .id = 2,
  152. .num_resources = ARRAY_SIZE(led2_resources),
  153. .resources = led2_resources,
  154. }, {
  155. .name = "88pm860x-led",
  156. .id = 3,
  157. .num_resources = ARRAY_SIZE(led3_resources),
  158. .resources = led3_resources,
  159. }, {
  160. .name = "88pm860x-led",
  161. .id = 4,
  162. .num_resources = ARRAY_SIZE(led4_resources),
  163. .resources = led4_resources,
  164. }, {
  165. .name = "88pm860x-led",
  166. .id = 5,
  167. .num_resources = ARRAY_SIZE(led5_resources),
  168. .resources = led5_resources,
  169. },
  170. };
  171. static struct mfd_cell regulator_devs[] = {
  172. {"88pm860x-regulator", 0,},
  173. {"88pm860x-regulator", 1,},
  174. {"88pm860x-regulator", 2,},
  175. {"88pm860x-regulator", 3,},
  176. {"88pm860x-regulator", 4,},
  177. {"88pm860x-regulator", 5,},
  178. {"88pm860x-regulator", 6,},
  179. {"88pm860x-regulator", 7,},
  180. {"88pm860x-regulator", 8,},
  181. {"88pm860x-regulator", 9,},
  182. {"88pm860x-regulator", 10,},
  183. {"88pm860x-regulator", 11,},
  184. {"88pm860x-regulator", 12,},
  185. {"88pm860x-regulator", 13,},
  186. {"88pm860x-regulator", 14,},
  187. {"88pm860x-regulator", 15,},
  188. {"88pm860x-regulator", 16,},
  189. {"88pm860x-regulator", 17,},
  190. };
  191. static struct mfd_cell touch_devs[] = {
  192. {"88pm860x-touch", -1,},
  193. };
  194. static struct mfd_cell onkey_devs[] = {
  195. {"88pm860x-onkey", -1,},
  196. };
  197. static struct mfd_cell codec_devs[] = {
  198. {"88pm860x-codec", -1,},
  199. };
  200. static struct regulator_consumer_supply preg_supply[] = {
  201. REGULATOR_SUPPLY("preg", "charger-manager"),
  202. };
  203. static struct regulator_init_data preg_init_data = {
  204. .num_consumer_supplies = ARRAY_SIZE(preg_supply),
  205. .consumer_supplies = &preg_supply[0],
  206. };
  207. static struct mfd_cell power_devs[] = {
  208. {"88pm860x-battery", -1,},
  209. {"88pm860x-charger", -1,},
  210. {"88pm860x-preg", -1,},
  211. };
  212. static struct mfd_cell rtc_devs[] = {
  213. {"88pm860x-rtc", -1,},
  214. };
  215. struct pm860x_irq_data {
  216. int reg;
  217. int mask_reg;
  218. int enable; /* enable or not */
  219. int offs; /* bit offset in mask register */
  220. };
  221. static struct pm860x_irq_data pm860x_irqs[] = {
  222. [PM8607_IRQ_ONKEY] = {
  223. .reg = PM8607_INT_STATUS1,
  224. .mask_reg = PM8607_INT_MASK_1,
  225. .offs = 1 << 0,
  226. },
  227. [PM8607_IRQ_EXTON] = {
  228. .reg = PM8607_INT_STATUS1,
  229. .mask_reg = PM8607_INT_MASK_1,
  230. .offs = 1 << 1,
  231. },
  232. [PM8607_IRQ_CHG] = {
  233. .reg = PM8607_INT_STATUS1,
  234. .mask_reg = PM8607_INT_MASK_1,
  235. .offs = 1 << 2,
  236. },
  237. [PM8607_IRQ_BAT] = {
  238. .reg = PM8607_INT_STATUS1,
  239. .mask_reg = PM8607_INT_MASK_1,
  240. .offs = 1 << 3,
  241. },
  242. [PM8607_IRQ_RTC] = {
  243. .reg = PM8607_INT_STATUS1,
  244. .mask_reg = PM8607_INT_MASK_1,
  245. .offs = 1 << 4,
  246. },
  247. [PM8607_IRQ_CC] = {
  248. .reg = PM8607_INT_STATUS1,
  249. .mask_reg = PM8607_INT_MASK_1,
  250. .offs = 1 << 5,
  251. },
  252. [PM8607_IRQ_VBAT] = {
  253. .reg = PM8607_INT_STATUS2,
  254. .mask_reg = PM8607_INT_MASK_2,
  255. .offs = 1 << 0,
  256. },
  257. [PM8607_IRQ_VCHG] = {
  258. .reg = PM8607_INT_STATUS2,
  259. .mask_reg = PM8607_INT_MASK_2,
  260. .offs = 1 << 1,
  261. },
  262. [PM8607_IRQ_VSYS] = {
  263. .reg = PM8607_INT_STATUS2,
  264. .mask_reg = PM8607_INT_MASK_2,
  265. .offs = 1 << 2,
  266. },
  267. [PM8607_IRQ_TINT] = {
  268. .reg = PM8607_INT_STATUS2,
  269. .mask_reg = PM8607_INT_MASK_2,
  270. .offs = 1 << 3,
  271. },
  272. [PM8607_IRQ_GPADC0] = {
  273. .reg = PM8607_INT_STATUS2,
  274. .mask_reg = PM8607_INT_MASK_2,
  275. .offs = 1 << 4,
  276. },
  277. [PM8607_IRQ_GPADC1] = {
  278. .reg = PM8607_INT_STATUS2,
  279. .mask_reg = PM8607_INT_MASK_2,
  280. .offs = 1 << 5,
  281. },
  282. [PM8607_IRQ_GPADC2] = {
  283. .reg = PM8607_INT_STATUS2,
  284. .mask_reg = PM8607_INT_MASK_2,
  285. .offs = 1 << 6,
  286. },
  287. [PM8607_IRQ_GPADC3] = {
  288. .reg = PM8607_INT_STATUS2,
  289. .mask_reg = PM8607_INT_MASK_2,
  290. .offs = 1 << 7,
  291. },
  292. [PM8607_IRQ_AUDIO_SHORT] = {
  293. .reg = PM8607_INT_STATUS3,
  294. .mask_reg = PM8607_INT_MASK_3,
  295. .offs = 1 << 0,
  296. },
  297. [PM8607_IRQ_PEN] = {
  298. .reg = PM8607_INT_STATUS3,
  299. .mask_reg = PM8607_INT_MASK_3,
  300. .offs = 1 << 1,
  301. },
  302. [PM8607_IRQ_HEADSET] = {
  303. .reg = PM8607_INT_STATUS3,
  304. .mask_reg = PM8607_INT_MASK_3,
  305. .offs = 1 << 2,
  306. },
  307. [PM8607_IRQ_HOOK] = {
  308. .reg = PM8607_INT_STATUS3,
  309. .mask_reg = PM8607_INT_MASK_3,
  310. .offs = 1 << 3,
  311. },
  312. [PM8607_IRQ_MICIN] = {
  313. .reg = PM8607_INT_STATUS3,
  314. .mask_reg = PM8607_INT_MASK_3,
  315. .offs = 1 << 4,
  316. },
  317. [PM8607_IRQ_CHG_FAIL] = {
  318. .reg = PM8607_INT_STATUS3,
  319. .mask_reg = PM8607_INT_MASK_3,
  320. .offs = 1 << 5,
  321. },
  322. [PM8607_IRQ_CHG_DONE] = {
  323. .reg = PM8607_INT_STATUS3,
  324. .mask_reg = PM8607_INT_MASK_3,
  325. .offs = 1 << 6,
  326. },
  327. [PM8607_IRQ_CHG_FAULT] = {
  328. .reg = PM8607_INT_STATUS3,
  329. .mask_reg = PM8607_INT_MASK_3,
  330. .offs = 1 << 7,
  331. },
  332. };
  333. static irqreturn_t pm860x_irq(int irq, void *data)
  334. {
  335. struct pm860x_chip *chip = data;
  336. struct pm860x_irq_data *irq_data;
  337. struct i2c_client *i2c;
  338. int read_reg = -1, value = 0;
  339. int i;
  340. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  341. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  342. irq_data = &pm860x_irqs[i];
  343. if (read_reg != irq_data->reg) {
  344. read_reg = irq_data->reg;
  345. value = pm860x_reg_read(i2c, irq_data->reg);
  346. }
  347. if (value & irq_data->enable)
  348. handle_nested_irq(chip->irq_base + i);
  349. }
  350. return IRQ_HANDLED;
  351. }
  352. static void pm860x_irq_lock(struct irq_data *data)
  353. {
  354. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  355. mutex_lock(&chip->irq_lock);
  356. }
  357. static void pm860x_irq_sync_unlock(struct irq_data *data)
  358. {
  359. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  360. struct pm860x_irq_data *irq_data;
  361. struct i2c_client *i2c;
  362. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  363. unsigned char mask[3];
  364. int i;
  365. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  366. /* Load cached value. In initial, all IRQs are masked */
  367. for (i = 0; i < 3; i++)
  368. mask[i] = cached[i];
  369. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  370. irq_data = &pm860x_irqs[i];
  371. switch (irq_data->mask_reg) {
  372. case PM8607_INT_MASK_1:
  373. mask[0] &= ~irq_data->offs;
  374. mask[0] |= irq_data->enable;
  375. break;
  376. case PM8607_INT_MASK_2:
  377. mask[1] &= ~irq_data->offs;
  378. mask[1] |= irq_data->enable;
  379. break;
  380. case PM8607_INT_MASK_3:
  381. mask[2] &= ~irq_data->offs;
  382. mask[2] |= irq_data->enable;
  383. break;
  384. default:
  385. dev_err(chip->dev, "wrong IRQ\n");
  386. break;
  387. }
  388. }
  389. /* update mask into registers */
  390. for (i = 0; i < 3; i++) {
  391. if (mask[i] != cached[i]) {
  392. cached[i] = mask[i];
  393. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  394. }
  395. }
  396. mutex_unlock(&chip->irq_lock);
  397. }
  398. static void pm860x_irq_enable(struct irq_data *data)
  399. {
  400. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  401. pm860x_irqs[data->irq - chip->irq_base].enable
  402. = pm860x_irqs[data->irq - chip->irq_base].offs;
  403. }
  404. static void pm860x_irq_disable(struct irq_data *data)
  405. {
  406. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  407. pm860x_irqs[data->irq - chip->irq_base].enable = 0;
  408. }
  409. static struct irq_chip pm860x_irq_chip = {
  410. .name = "88pm860x",
  411. .irq_bus_lock = pm860x_irq_lock,
  412. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  413. .irq_enable = pm860x_irq_enable,
  414. .irq_disable = pm860x_irq_disable,
  415. };
  416. static int __devinit device_gpadc_init(struct pm860x_chip *chip,
  417. struct pm860x_platform_data *pdata)
  418. {
  419. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  420. : chip->companion;
  421. int data;
  422. int ret;
  423. /* initialize GPADC without activating it */
  424. if (!pdata || !pdata->touch)
  425. return -EINVAL;
  426. /* set GPADC MISC1 register */
  427. data = 0;
  428. data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
  429. data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  430. data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  431. data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
  432. if (data) {
  433. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  434. if (ret < 0)
  435. goto out;
  436. }
  437. /* set tsi prebias time */
  438. if (pdata->touch->tsi_prebias) {
  439. data = pdata->touch->tsi_prebias;
  440. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  441. if (ret < 0)
  442. goto out;
  443. }
  444. /* set prebias & prechg time of pen detect */
  445. data = 0;
  446. data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
  447. data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
  448. if (data) {
  449. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  450. if (ret < 0)
  451. goto out;
  452. }
  453. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
  454. PM8607_GPADC_EN, PM8607_GPADC_EN);
  455. out:
  456. return ret;
  457. }
  458. static int __devinit device_irq_init(struct pm860x_chip *chip,
  459. struct pm860x_platform_data *pdata)
  460. {
  461. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  462. : chip->companion;
  463. unsigned char status_buf[INT_STATUS_NUM];
  464. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  465. int i, data, mask, ret = -EINVAL;
  466. int __irq;
  467. if (!pdata || !pdata->irq_base) {
  468. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  469. return -EINVAL;
  470. }
  471. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  472. | PM8607_B0_MISC1_INT_MASK;
  473. data = 0;
  474. chip->irq_mode = 0;
  475. if (pdata && pdata->irq_mode) {
  476. /*
  477. * irq_mode defines the way of clearing interrupt. If it's 1,
  478. * clear IRQ by write. Otherwise, clear it by read.
  479. * This control bit is valid from 88PM8607 B0 steping.
  480. */
  481. data |= PM8607_B0_MISC1_INT_CLEAR;
  482. chip->irq_mode = 1;
  483. }
  484. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  485. if (ret < 0)
  486. goto out;
  487. /* mask all IRQs */
  488. memset(status_buf, 0, INT_STATUS_NUM);
  489. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  490. INT_STATUS_NUM, status_buf);
  491. if (ret < 0)
  492. goto out;
  493. if (chip->irq_mode) {
  494. /* clear interrupt status by write */
  495. memset(status_buf, 0xFF, INT_STATUS_NUM);
  496. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  497. INT_STATUS_NUM, status_buf);
  498. } else {
  499. /* clear interrupt status by read */
  500. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  501. INT_STATUS_NUM, status_buf);
  502. }
  503. if (ret < 0)
  504. goto out;
  505. mutex_init(&chip->irq_lock);
  506. chip->irq_base = pdata->irq_base;
  507. chip->core_irq = i2c->irq;
  508. if (!chip->core_irq)
  509. goto out;
  510. /* register IRQ by genirq */
  511. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  512. __irq = i + chip->irq_base;
  513. irq_set_chip_data(__irq, chip);
  514. irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
  515. handle_edge_irq);
  516. irq_set_nested_thread(__irq, 1);
  517. #ifdef CONFIG_ARM
  518. set_irq_flags(__irq, IRQF_VALID);
  519. #else
  520. irq_set_noprobe(__irq);
  521. #endif
  522. }
  523. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
  524. "88pm860x", chip);
  525. if (ret) {
  526. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  527. chip->core_irq = 0;
  528. }
  529. return 0;
  530. out:
  531. chip->core_irq = 0;
  532. return ret;
  533. }
  534. static void device_irq_exit(struct pm860x_chip *chip)
  535. {
  536. if (chip->core_irq)
  537. free_irq(chip->core_irq, chip);
  538. }
  539. int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
  540. {
  541. int ret = -EIO;
  542. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  543. chip->client : chip->companion;
  544. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  545. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  546. __func__, chip->osc_vote,
  547. chip->osc_status);
  548. mutex_lock(&chip->osc_lock);
  549. /* Update voting status */
  550. chip->osc_vote |= client;
  551. /* If reference group is off - turn on*/
  552. if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
  553. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  554. /* Enable Reference group Vsys */
  555. if (pm860x_set_bits(i2c, PM8606_VSYS,
  556. PM8606_VSYS_EN, PM8606_VSYS_EN))
  557. goto out;
  558. /*Enable Internal Oscillator */
  559. if (pm860x_set_bits(i2c, PM8606_MISC,
  560. PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
  561. goto out;
  562. /* Update status (only if writes succeed) */
  563. chip->osc_status = PM8606_REF_GP_OSC_ON;
  564. }
  565. mutex_unlock(&chip->osc_lock);
  566. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  567. __func__, chip->osc_vote,
  568. chip->osc_status, ret);
  569. return 0;
  570. out:
  571. mutex_unlock(&chip->osc_lock);
  572. return ret;
  573. }
  574. EXPORT_SYMBOL(pm8606_osc_enable);
  575. int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
  576. {
  577. int ret = -EIO;
  578. struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
  579. chip->client : chip->companion;
  580. dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
  581. dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
  582. __func__, chip->osc_vote,
  583. chip->osc_status);
  584. mutex_lock(&chip->osc_lock);
  585. /*Update voting status */
  586. chip->osc_vote &= ~(client);
  587. /* If reference group is off and this is the last client to release
  588. * - turn off */
  589. if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
  590. (chip->osc_vote == REF_GP_NO_CLIENTS)) {
  591. chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
  592. /* Disable Reference group Vsys */
  593. if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
  594. goto out;
  595. /* Disable Internal Oscillator */
  596. if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
  597. goto out;
  598. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  599. }
  600. mutex_unlock(&chip->osc_lock);
  601. dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
  602. __func__, chip->osc_vote,
  603. chip->osc_status, ret);
  604. return 0;
  605. out:
  606. mutex_unlock(&chip->osc_lock);
  607. return ret;
  608. }
  609. EXPORT_SYMBOL(pm8606_osc_disable);
  610. static void __devinit device_osc_init(struct i2c_client *i2c)
  611. {
  612. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  613. mutex_init(&chip->osc_lock);
  614. /* init portofino reference group voting and status */
  615. /* Disable Reference group Vsys */
  616. pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
  617. /* Disable Internal Oscillator */
  618. pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
  619. chip->osc_vote = REF_GP_NO_CLIENTS;
  620. chip->osc_status = PM8606_REF_GP_OSC_OFF;
  621. }
  622. static void __devinit device_bk_init(struct pm860x_chip *chip,
  623. struct pm860x_platform_data *pdata)
  624. {
  625. int ret, i;
  626. if (pdata && pdata->backlight) {
  627. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  628. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  629. for (i = 0; i < pdata->num_backlights; i++) {
  630. bk_devs[i].platform_data = &pdata->backlight[i];
  631. bk_devs[i].pdata_size =
  632. sizeof(struct pm860x_backlight_pdata);
  633. }
  634. }
  635. ret = mfd_add_devices(chip->dev, 0, bk_devs,
  636. ARRAY_SIZE(bk_devs), NULL, 0);
  637. if (ret < 0)
  638. dev_err(chip->dev, "Failed to add backlight subdev\n");
  639. }
  640. static void __devinit device_led_init(struct pm860x_chip *chip,
  641. struct pm860x_platform_data *pdata)
  642. {
  643. int ret, i;
  644. if (pdata && pdata->led) {
  645. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  646. pdata->num_leds = ARRAY_SIZE(led_devs);
  647. for (i = 0; i < pdata->num_leds; i++) {
  648. led_devs[i].platform_data = &pdata->led[i];
  649. led_devs[i].pdata_size =
  650. sizeof(struct pm860x_led_pdata);
  651. }
  652. }
  653. ret = mfd_add_devices(chip->dev, 0, led_devs,
  654. ARRAY_SIZE(led_devs), NULL, 0);
  655. if (ret < 0) {
  656. dev_err(chip->dev, "Failed to add led subdev\n");
  657. return;
  658. }
  659. }
  660. static void __devinit device_regulator_init(struct pm860x_chip *chip,
  661. struct pm860x_platform_data *pdata)
  662. {
  663. struct regulator_init_data *initdata;
  664. int ret;
  665. int i, seq;
  666. if ((pdata == NULL) || (pdata->regulator == NULL))
  667. return;
  668. if (pdata->num_regulators > ARRAY_SIZE(regulator_devs))
  669. pdata->num_regulators = ARRAY_SIZE(regulator_devs);
  670. for (i = 0, seq = -1; i < pdata->num_regulators; i++) {
  671. initdata = &pdata->regulator[i];
  672. seq = *(unsigned int *)initdata->driver_data;
  673. if ((seq < 0) || (seq > PM8607_ID_RG_MAX)) {
  674. dev_err(chip->dev, "Wrong ID(%d) on regulator(%s)\n",
  675. seq, initdata->constraints.name);
  676. goto out;
  677. }
  678. regulator_devs[i].platform_data = &pdata->regulator[i];
  679. regulator_devs[i].pdata_size = sizeof(struct regulator_init_data);
  680. regulator_devs[i].num_resources = 1;
  681. regulator_devs[i].resources = &regulator_resources[seq];
  682. ret = mfd_add_devices(chip->dev, 0, &regulator_devs[i], 1,
  683. &regulator_resources[seq], 0);
  684. if (ret < 0) {
  685. dev_err(chip->dev, "Failed to add regulator subdev\n");
  686. goto out;
  687. }
  688. }
  689. out:
  690. return;
  691. }
  692. static void __devinit device_rtc_init(struct pm860x_chip *chip,
  693. struct pm860x_platform_data *pdata)
  694. {
  695. int ret;
  696. if ((pdata == NULL))
  697. return;
  698. rtc_devs[0].platform_data = pdata->rtc;
  699. rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
  700. rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
  701. rtc_devs[0].resources = &rtc_resources[0];
  702. ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
  703. ARRAY_SIZE(rtc_devs), &rtc_resources[0],
  704. chip->irq_base);
  705. if (ret < 0)
  706. dev_err(chip->dev, "Failed to add rtc subdev\n");
  707. }
  708. static void __devinit device_touch_init(struct pm860x_chip *chip,
  709. struct pm860x_platform_data *pdata)
  710. {
  711. int ret;
  712. if (pdata == NULL)
  713. return;
  714. touch_devs[0].platform_data = pdata->touch;
  715. touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
  716. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  717. touch_devs[0].resources = &touch_resources[0];
  718. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  719. ARRAY_SIZE(touch_devs), &touch_resources[0],
  720. chip->irq_base);
  721. if (ret < 0)
  722. dev_err(chip->dev, "Failed to add touch subdev\n");
  723. }
  724. static void __devinit device_power_init(struct pm860x_chip *chip,
  725. struct pm860x_platform_data *pdata)
  726. {
  727. int ret;
  728. if (pdata == NULL)
  729. return;
  730. power_devs[0].platform_data = pdata->power;
  731. power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
  732. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  733. power_devs[0].resources = &battery_resources[0],
  734. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  735. &battery_resources[0], chip->irq_base);
  736. if (ret < 0)
  737. dev_err(chip->dev, "Failed to add battery subdev\n");
  738. power_devs[1].platform_data = pdata->power;
  739. power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
  740. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  741. power_devs[1].resources = &charger_resources[0],
  742. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  743. &charger_resources[0], chip->irq_base);
  744. if (ret < 0)
  745. dev_err(chip->dev, "Failed to add charger subdev\n");
  746. power_devs[2].platform_data = &preg_init_data;
  747. power_devs[2].pdata_size = sizeof(struct regulator_init_data);
  748. power_devs[2].num_resources = ARRAY_SIZE(preg_resources);
  749. power_devs[2].resources = &preg_resources[0],
  750. ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
  751. &preg_resources[0], chip->irq_base);
  752. if (ret < 0)
  753. dev_err(chip->dev, "Failed to add preg subdev\n");
  754. }
  755. static void __devinit device_onkey_init(struct pm860x_chip *chip,
  756. struct pm860x_platform_data *pdata)
  757. {
  758. int ret;
  759. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  760. onkey_devs[0].resources = &onkey_resources[0],
  761. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  762. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  763. chip->irq_base);
  764. if (ret < 0)
  765. dev_err(chip->dev, "Failed to add onkey subdev\n");
  766. }
  767. static void __devinit device_codec_init(struct pm860x_chip *chip,
  768. struct pm860x_platform_data *pdata)
  769. {
  770. int ret;
  771. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  772. codec_devs[0].resources = &codec_resources[0],
  773. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  774. ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
  775. if (ret < 0)
  776. dev_err(chip->dev, "Failed to add codec subdev\n");
  777. }
  778. static void __devinit device_8607_init(struct pm860x_chip *chip,
  779. struct i2c_client *i2c,
  780. struct pm860x_platform_data *pdata)
  781. {
  782. int data, ret;
  783. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  784. if (ret < 0) {
  785. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  786. goto out;
  787. }
  788. switch (ret & PM8607_VERSION_MASK) {
  789. case 0x40:
  790. case 0x50:
  791. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  792. ret);
  793. break;
  794. default:
  795. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  796. "Chip ID: %02x\n", ret);
  797. goto out;
  798. }
  799. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  800. if (ret < 0) {
  801. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  802. goto out;
  803. }
  804. if (ret & PM8607_BUCK3_DOUBLE)
  805. chip->buck3_double = 1;
  806. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  807. if (ret < 0) {
  808. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  809. goto out;
  810. }
  811. if (pdata && (pdata->i2c_port == PI2C_PORT))
  812. data = PM8607_B0_MISC1_PI2C;
  813. else
  814. data = 0;
  815. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  816. if (ret < 0) {
  817. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  818. goto out;
  819. }
  820. ret = device_gpadc_init(chip, pdata);
  821. if (ret < 0)
  822. goto out;
  823. ret = device_irq_init(chip, pdata);
  824. if (ret < 0)
  825. goto out;
  826. device_regulator_init(chip, pdata);
  827. device_rtc_init(chip, pdata);
  828. device_onkey_init(chip, pdata);
  829. device_touch_init(chip, pdata);
  830. device_power_init(chip, pdata);
  831. device_codec_init(chip, pdata);
  832. out:
  833. return;
  834. }
  835. static void __devinit device_8606_init(struct pm860x_chip *chip,
  836. struct i2c_client *i2c,
  837. struct pm860x_platform_data *pdata)
  838. {
  839. device_osc_init(i2c);
  840. device_bk_init(chip, pdata);
  841. device_led_init(chip, pdata);
  842. }
  843. int __devinit pm860x_device_init(struct pm860x_chip *chip,
  844. struct pm860x_platform_data *pdata)
  845. {
  846. chip->core_irq = 0;
  847. switch (chip->id) {
  848. case CHIP_PM8606:
  849. device_8606_init(chip, chip->client, pdata);
  850. break;
  851. case CHIP_PM8607:
  852. device_8607_init(chip, chip->client, pdata);
  853. break;
  854. }
  855. if (chip->companion) {
  856. switch (chip->id) {
  857. case CHIP_PM8607:
  858. device_8606_init(chip, chip->companion, pdata);
  859. break;
  860. case CHIP_PM8606:
  861. device_8607_init(chip, chip->companion, pdata);
  862. break;
  863. }
  864. }
  865. return 0;
  866. }
  867. void __devexit pm860x_device_exit(struct pm860x_chip *chip)
  868. {
  869. device_irq_exit(chip);
  870. mfd_remove_devices(chip->dev);
  871. }
  872. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  873. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  874. MODULE_LICENSE("GPL");