88pm860x-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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 bk_resources[] __initdata = {
  22. {PM8606_BACKLIGHT1, PM8606_BACKLIGHT1, "backlight-0", IORESOURCE_IO,},
  23. {PM8606_BACKLIGHT2, PM8606_BACKLIGHT2, "backlight-1", IORESOURCE_IO,},
  24. {PM8606_BACKLIGHT3, PM8606_BACKLIGHT3, "backlight-2", IORESOURCE_IO,},
  25. };
  26. static struct resource led_resources[] __initdata = {
  27. {PM8606_LED1_RED, PM8606_LED1_RED, "led0-red", IORESOURCE_IO,},
  28. {PM8606_LED1_GREEN, PM8606_LED1_GREEN, "led0-green", IORESOURCE_IO,},
  29. {PM8606_LED1_BLUE, PM8606_LED1_BLUE, "led0-blue", IORESOURCE_IO,},
  30. {PM8606_LED2_RED, PM8606_LED2_RED, "led1-red", IORESOURCE_IO,},
  31. {PM8606_LED2_GREEN, PM8606_LED2_GREEN, "led1-green", IORESOURCE_IO,},
  32. {PM8606_LED2_BLUE, PM8606_LED2_BLUE, "led1-blue", IORESOURCE_IO,},
  33. };
  34. static struct resource regulator_resources[] __initdata = {
  35. {PM8607_ID_BUCK1, PM8607_ID_BUCK1, "buck-1", IORESOURCE_IO,},
  36. {PM8607_ID_BUCK2, PM8607_ID_BUCK2, "buck-2", IORESOURCE_IO,},
  37. {PM8607_ID_BUCK3, PM8607_ID_BUCK3, "buck-3", IORESOURCE_IO,},
  38. {PM8607_ID_LDO1, PM8607_ID_LDO1, "ldo-01", IORESOURCE_IO,},
  39. {PM8607_ID_LDO2, PM8607_ID_LDO2, "ldo-02", IORESOURCE_IO,},
  40. {PM8607_ID_LDO3, PM8607_ID_LDO3, "ldo-03", IORESOURCE_IO,},
  41. {PM8607_ID_LDO4, PM8607_ID_LDO4, "ldo-04", IORESOURCE_IO,},
  42. {PM8607_ID_LDO5, PM8607_ID_LDO5, "ldo-05", IORESOURCE_IO,},
  43. {PM8607_ID_LDO6, PM8607_ID_LDO6, "ldo-06", IORESOURCE_IO,},
  44. {PM8607_ID_LDO7, PM8607_ID_LDO7, "ldo-07", IORESOURCE_IO,},
  45. {PM8607_ID_LDO8, PM8607_ID_LDO8, "ldo-08", IORESOURCE_IO,},
  46. {PM8607_ID_LDO9, PM8607_ID_LDO9, "ldo-09", IORESOURCE_IO,},
  47. {PM8607_ID_LDO10, PM8607_ID_LDO10, "ldo-10", IORESOURCE_IO,},
  48. {PM8607_ID_LDO11, PM8607_ID_LDO11, "ldo-11", IORESOURCE_IO,},
  49. {PM8607_ID_LDO12, PM8607_ID_LDO12, "ldo-12", IORESOURCE_IO,},
  50. {PM8607_ID_LDO13, PM8607_ID_LDO13, "ldo-13", IORESOURCE_IO,},
  51. {PM8607_ID_LDO14, PM8607_ID_LDO14, "ldo-14", IORESOURCE_IO,},
  52. {PM8607_ID_LDO15, PM8607_ID_LDO15, "ldo-15", IORESOURCE_IO,},
  53. };
  54. static struct resource touch_resources[] __initdata = {
  55. {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
  56. };
  57. static struct resource onkey_resources[] __initdata = {
  58. {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
  59. };
  60. static struct resource codec_resources[] __initdata = {
  61. /* Headset microphone insertion or removal */
  62. {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
  63. /* Hook-switch press or release */
  64. {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
  65. /* Headset insertion or removal */
  66. {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
  67. /* Audio short */
  68. {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
  69. };
  70. static struct resource battery_resources[] __initdata = {
  71. {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
  72. {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
  73. };
  74. static struct resource charger_resources[] __initdata = {
  75. {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
  76. {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,},
  77. {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,},
  78. {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,},
  79. {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
  80. {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
  81. };
  82. static struct mfd_cell bk_devs[] __initdata = {
  83. {"88pm860x-backlight", 0,},
  84. {"88pm860x-backlight", 1,},
  85. {"88pm860x-backlight", 2,},
  86. };
  87. static struct mfd_cell led_devs[] __initdata = {
  88. {"88pm860x-led", 0,},
  89. {"88pm860x-led", 1,},
  90. {"88pm860x-led", 2,},
  91. {"88pm860x-led", 3,},
  92. {"88pm860x-led", 4,},
  93. {"88pm860x-led", 5,},
  94. };
  95. static struct mfd_cell regulator_devs[] __initdata = {
  96. {"88pm860x-regulator", 0,},
  97. {"88pm860x-regulator", 1,},
  98. {"88pm860x-regulator", 2,},
  99. {"88pm860x-regulator", 3,},
  100. {"88pm860x-regulator", 4,},
  101. {"88pm860x-regulator", 5,},
  102. {"88pm860x-regulator", 6,},
  103. {"88pm860x-regulator", 7,},
  104. {"88pm860x-regulator", 8,},
  105. {"88pm860x-regulator", 9,},
  106. {"88pm860x-regulator", 10,},
  107. {"88pm860x-regulator", 11,},
  108. {"88pm860x-regulator", 12,},
  109. {"88pm860x-regulator", 13,},
  110. {"88pm860x-regulator", 14,},
  111. {"88pm860x-regulator", 15,},
  112. {"88pm860x-regulator", 16,},
  113. {"88pm860x-regulator", 17,},
  114. };
  115. static struct mfd_cell touch_devs[] __initdata = {
  116. {"88pm860x-touch", -1,},
  117. };
  118. static struct mfd_cell onkey_devs[] __initdata = {
  119. {"88pm860x-onkey", -1,},
  120. };
  121. static struct mfd_cell codec_devs[] __initdata = {
  122. {"88pm860x-codec", -1,},
  123. };
  124. static struct mfd_cell power_devs[] = {
  125. {"88pm860x-battery", -1,},
  126. {"88pm860x-charger", -1,},
  127. };
  128. static struct pm860x_backlight_pdata bk_pdata[ARRAY_SIZE(bk_devs)];
  129. static struct pm860x_led_pdata led_pdata[ARRAY_SIZE(led_devs)];
  130. static struct regulator_init_data regulator_pdata[ARRAY_SIZE(regulator_devs)];
  131. static struct pm860x_touch_pdata touch_pdata;
  132. static struct pm860x_power_pdata power_pdata;
  133. struct pm860x_irq_data {
  134. int reg;
  135. int mask_reg;
  136. int enable; /* enable or not */
  137. int offs; /* bit offset in mask register */
  138. };
  139. static struct pm860x_irq_data pm860x_irqs[] = {
  140. [PM8607_IRQ_ONKEY] = {
  141. .reg = PM8607_INT_STATUS1,
  142. .mask_reg = PM8607_INT_MASK_1,
  143. .offs = 1 << 0,
  144. },
  145. [PM8607_IRQ_EXTON] = {
  146. .reg = PM8607_INT_STATUS1,
  147. .mask_reg = PM8607_INT_MASK_1,
  148. .offs = 1 << 1,
  149. },
  150. [PM8607_IRQ_CHG] = {
  151. .reg = PM8607_INT_STATUS1,
  152. .mask_reg = PM8607_INT_MASK_1,
  153. .offs = 1 << 2,
  154. },
  155. [PM8607_IRQ_BAT] = {
  156. .reg = PM8607_INT_STATUS1,
  157. .mask_reg = PM8607_INT_MASK_1,
  158. .offs = 1 << 3,
  159. },
  160. [PM8607_IRQ_RTC] = {
  161. .reg = PM8607_INT_STATUS1,
  162. .mask_reg = PM8607_INT_MASK_1,
  163. .offs = 1 << 4,
  164. },
  165. [PM8607_IRQ_CC] = {
  166. .reg = PM8607_INT_STATUS1,
  167. .mask_reg = PM8607_INT_MASK_1,
  168. .offs = 1 << 5,
  169. },
  170. [PM8607_IRQ_VBAT] = {
  171. .reg = PM8607_INT_STATUS2,
  172. .mask_reg = PM8607_INT_MASK_2,
  173. .offs = 1 << 0,
  174. },
  175. [PM8607_IRQ_VCHG] = {
  176. .reg = PM8607_INT_STATUS2,
  177. .mask_reg = PM8607_INT_MASK_2,
  178. .offs = 1 << 1,
  179. },
  180. [PM8607_IRQ_VSYS] = {
  181. .reg = PM8607_INT_STATUS2,
  182. .mask_reg = PM8607_INT_MASK_2,
  183. .offs = 1 << 2,
  184. },
  185. [PM8607_IRQ_TINT] = {
  186. .reg = PM8607_INT_STATUS2,
  187. .mask_reg = PM8607_INT_MASK_2,
  188. .offs = 1 << 3,
  189. },
  190. [PM8607_IRQ_GPADC0] = {
  191. .reg = PM8607_INT_STATUS2,
  192. .mask_reg = PM8607_INT_MASK_2,
  193. .offs = 1 << 4,
  194. },
  195. [PM8607_IRQ_GPADC1] = {
  196. .reg = PM8607_INT_STATUS2,
  197. .mask_reg = PM8607_INT_MASK_2,
  198. .offs = 1 << 5,
  199. },
  200. [PM8607_IRQ_GPADC2] = {
  201. .reg = PM8607_INT_STATUS2,
  202. .mask_reg = PM8607_INT_MASK_2,
  203. .offs = 1 << 6,
  204. },
  205. [PM8607_IRQ_GPADC3] = {
  206. .reg = PM8607_INT_STATUS2,
  207. .mask_reg = PM8607_INT_MASK_2,
  208. .offs = 1 << 7,
  209. },
  210. [PM8607_IRQ_AUDIO_SHORT] = {
  211. .reg = PM8607_INT_STATUS3,
  212. .mask_reg = PM8607_INT_MASK_3,
  213. .offs = 1 << 0,
  214. },
  215. [PM8607_IRQ_PEN] = {
  216. .reg = PM8607_INT_STATUS3,
  217. .mask_reg = PM8607_INT_MASK_3,
  218. .offs = 1 << 1,
  219. },
  220. [PM8607_IRQ_HEADSET] = {
  221. .reg = PM8607_INT_STATUS3,
  222. .mask_reg = PM8607_INT_MASK_3,
  223. .offs = 1 << 2,
  224. },
  225. [PM8607_IRQ_HOOK] = {
  226. .reg = PM8607_INT_STATUS3,
  227. .mask_reg = PM8607_INT_MASK_3,
  228. .offs = 1 << 3,
  229. },
  230. [PM8607_IRQ_MICIN] = {
  231. .reg = PM8607_INT_STATUS3,
  232. .mask_reg = PM8607_INT_MASK_3,
  233. .offs = 1 << 4,
  234. },
  235. [PM8607_IRQ_CHG_FAIL] = {
  236. .reg = PM8607_INT_STATUS3,
  237. .mask_reg = PM8607_INT_MASK_3,
  238. .offs = 1 << 5,
  239. },
  240. [PM8607_IRQ_CHG_DONE] = {
  241. .reg = PM8607_INT_STATUS3,
  242. .mask_reg = PM8607_INT_MASK_3,
  243. .offs = 1 << 6,
  244. },
  245. [PM8607_IRQ_CHG_FAULT] = {
  246. .reg = PM8607_INT_STATUS3,
  247. .mask_reg = PM8607_INT_MASK_3,
  248. .offs = 1 << 7,
  249. },
  250. };
  251. static irqreturn_t pm860x_irq(int irq, void *data)
  252. {
  253. struct pm860x_chip *chip = data;
  254. struct pm860x_irq_data *irq_data;
  255. struct i2c_client *i2c;
  256. int read_reg = -1, value = 0;
  257. int i;
  258. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  259. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  260. irq_data = &pm860x_irqs[i];
  261. if (read_reg != irq_data->reg) {
  262. read_reg = irq_data->reg;
  263. value = pm860x_reg_read(i2c, irq_data->reg);
  264. }
  265. if (value & irq_data->enable)
  266. handle_nested_irq(chip->irq_base + i);
  267. }
  268. return IRQ_HANDLED;
  269. }
  270. static void pm860x_irq_lock(struct irq_data *data)
  271. {
  272. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  273. mutex_lock(&chip->irq_lock);
  274. }
  275. static void pm860x_irq_sync_unlock(struct irq_data *data)
  276. {
  277. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  278. struct pm860x_irq_data *irq_data;
  279. struct i2c_client *i2c;
  280. static unsigned char cached[3] = {0x0, 0x0, 0x0};
  281. unsigned char mask[3];
  282. int i;
  283. i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  284. /* Load cached value. In initial, all IRQs are masked */
  285. for (i = 0; i < 3; i++)
  286. mask[i] = cached[i];
  287. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  288. irq_data = &pm860x_irqs[i];
  289. switch (irq_data->mask_reg) {
  290. case PM8607_INT_MASK_1:
  291. mask[0] &= ~irq_data->offs;
  292. mask[0] |= irq_data->enable;
  293. break;
  294. case PM8607_INT_MASK_2:
  295. mask[1] &= ~irq_data->offs;
  296. mask[1] |= irq_data->enable;
  297. break;
  298. case PM8607_INT_MASK_3:
  299. mask[2] &= ~irq_data->offs;
  300. mask[2] |= irq_data->enable;
  301. break;
  302. default:
  303. dev_err(chip->dev, "wrong IRQ\n");
  304. break;
  305. }
  306. }
  307. /* update mask into registers */
  308. for (i = 0; i < 3; i++) {
  309. if (mask[i] != cached[i]) {
  310. cached[i] = mask[i];
  311. pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
  312. }
  313. }
  314. mutex_unlock(&chip->irq_lock);
  315. }
  316. static void pm860x_irq_enable(struct irq_data *data)
  317. {
  318. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  319. pm860x_irqs[data->irq - chip->irq_base].enable
  320. = pm860x_irqs[data->irq - chip->irq_base].offs;
  321. }
  322. static void pm860x_irq_disable(struct irq_data *data)
  323. {
  324. struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
  325. pm860x_irqs[data->irq - chip->irq_base].enable = 0;
  326. }
  327. static struct irq_chip pm860x_irq_chip = {
  328. .name = "88pm860x",
  329. .irq_bus_lock = pm860x_irq_lock,
  330. .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
  331. .irq_enable = pm860x_irq_enable,
  332. .irq_disable = pm860x_irq_disable,
  333. };
  334. static int __devinit device_gpadc_init(struct pm860x_chip *chip,
  335. struct pm860x_platform_data *pdata)
  336. {
  337. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  338. : chip->companion;
  339. int data;
  340. int ret;
  341. /* initialize GPADC without activating it */
  342. if (!pdata || !pdata->touch)
  343. return -EINVAL;
  344. /* set GPADC MISC1 register */
  345. data = 0;
  346. data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
  347. data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  348. data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  349. data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
  350. if (data) {
  351. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  352. if (ret < 0)
  353. goto out;
  354. }
  355. /* set tsi prebias time */
  356. if (pdata->touch->tsi_prebias) {
  357. data = pdata->touch->tsi_prebias;
  358. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  359. if (ret < 0)
  360. goto out;
  361. }
  362. /* set prebias & prechg time of pen detect */
  363. data = 0;
  364. data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
  365. data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
  366. if (data) {
  367. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  368. if (ret < 0)
  369. goto out;
  370. }
  371. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
  372. PM8607_GPADC_EN, PM8607_GPADC_EN);
  373. out:
  374. return ret;
  375. }
  376. static int __devinit device_irq_init(struct pm860x_chip *chip,
  377. struct pm860x_platform_data *pdata)
  378. {
  379. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  380. : chip->companion;
  381. unsigned char status_buf[INT_STATUS_NUM];
  382. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  383. int i, data, mask, ret = -EINVAL;
  384. int __irq;
  385. if (!pdata || !pdata->irq_base) {
  386. dev_warn(chip->dev, "No interrupt support on IRQ base\n");
  387. return -EINVAL;
  388. }
  389. mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
  390. | PM8607_B0_MISC1_INT_MASK;
  391. data = 0;
  392. chip->irq_mode = 0;
  393. if (pdata && pdata->irq_mode) {
  394. /*
  395. * irq_mode defines the way of clearing interrupt. If it's 1,
  396. * clear IRQ by write. Otherwise, clear it by read.
  397. * This control bit is valid from 88PM8607 B0 steping.
  398. */
  399. data |= PM8607_B0_MISC1_INT_CLEAR;
  400. chip->irq_mode = 1;
  401. }
  402. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
  403. if (ret < 0)
  404. goto out;
  405. /* mask all IRQs */
  406. memset(status_buf, 0, INT_STATUS_NUM);
  407. ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
  408. INT_STATUS_NUM, status_buf);
  409. if (ret < 0)
  410. goto out;
  411. if (chip->irq_mode) {
  412. /* clear interrupt status by write */
  413. memset(status_buf, 0xFF, INT_STATUS_NUM);
  414. ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
  415. INT_STATUS_NUM, status_buf);
  416. } else {
  417. /* clear interrupt status by read */
  418. ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
  419. INT_STATUS_NUM, status_buf);
  420. }
  421. if (ret < 0)
  422. goto out;
  423. mutex_init(&chip->irq_lock);
  424. chip->irq_base = pdata->irq_base;
  425. chip->core_irq = i2c->irq;
  426. if (!chip->core_irq)
  427. goto out;
  428. /* register IRQ by genirq */
  429. for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
  430. __irq = i + chip->irq_base;
  431. irq_set_chip_data(__irq, chip);
  432. irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
  433. handle_edge_irq);
  434. irq_set_nested_thread(__irq, 1);
  435. #ifdef CONFIG_ARM
  436. set_irq_flags(__irq, IRQF_VALID);
  437. #else
  438. irq_set_noprobe(__irq);
  439. #endif
  440. }
  441. ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
  442. "88pm860x", chip);
  443. if (ret) {
  444. dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
  445. chip->core_irq = 0;
  446. }
  447. return 0;
  448. out:
  449. chip->core_irq = 0;
  450. return ret;
  451. }
  452. static void device_irq_exit(struct pm860x_chip *chip)
  453. {
  454. if (chip->core_irq)
  455. free_irq(chip->core_irq, chip);
  456. }
  457. static void __devinit device_bk_init(struct pm860x_chip *chip,
  458. struct i2c_client *i2c,
  459. struct pm860x_platform_data *pdata)
  460. {
  461. int ret;
  462. int i, j, id;
  463. if ((pdata == NULL) || (pdata->backlight == NULL))
  464. return;
  465. if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
  466. pdata->num_backlights = ARRAY_SIZE(bk_devs);
  467. for (i = 0; i < pdata->num_backlights; i++) {
  468. memcpy(&bk_pdata[i], &pdata->backlight[i],
  469. sizeof(struct pm860x_backlight_pdata));
  470. bk_devs[i].mfd_data = &bk_pdata[i];
  471. for (j = 0; j < ARRAY_SIZE(bk_devs); j++) {
  472. id = bk_resources[j].start;
  473. if (bk_pdata[i].flags != id)
  474. continue;
  475. bk_devs[i].num_resources = 1;
  476. bk_devs[i].resources = &bk_resources[j];
  477. ret = mfd_add_devices(chip->dev, 0,
  478. &bk_devs[i], 1,
  479. &bk_resources[j], 0);
  480. if (ret < 0) {
  481. dev_err(chip->dev, "Failed to add "
  482. "backlight subdev\n");
  483. return;
  484. }
  485. }
  486. }
  487. }
  488. static void __devinit device_led_init(struct pm860x_chip *chip,
  489. struct i2c_client *i2c,
  490. struct pm860x_platform_data *pdata)
  491. {
  492. int ret;
  493. int i, j, id;
  494. if ((pdata == NULL) || (pdata->led == NULL))
  495. return;
  496. if (pdata->num_leds > ARRAY_SIZE(led_devs))
  497. pdata->num_leds = ARRAY_SIZE(led_devs);
  498. for (i = 0; i < pdata->num_leds; i++) {
  499. memcpy(&led_pdata[i], &pdata->led[i],
  500. sizeof(struct pm860x_led_pdata));
  501. led_devs[i].mfd_data = &led_pdata[i];
  502. for (j = 0; j < ARRAY_SIZE(led_devs); j++) {
  503. id = led_resources[j].start;
  504. if (led_pdata[i].flags != id)
  505. continue;
  506. led_devs[i].num_resources = 1;
  507. led_devs[i].resources = &led_resources[j],
  508. ret = mfd_add_devices(chip->dev, 0,
  509. &led_devs[i], 1,
  510. &led_resources[j], 0);
  511. if (ret < 0) {
  512. dev_err(chip->dev, "Failed to add "
  513. "led subdev\n");
  514. return;
  515. }
  516. }
  517. }
  518. }
  519. static void __devinit device_regulator_init(struct pm860x_chip *chip,
  520. struct i2c_client *i2c,
  521. struct pm860x_platform_data *pdata)
  522. {
  523. struct regulator_init_data *initdata;
  524. int ret;
  525. int i, j;
  526. if ((pdata == NULL) || (pdata->regulator == NULL))
  527. return;
  528. if (pdata->num_regulators > ARRAY_SIZE(regulator_devs))
  529. pdata->num_regulators = ARRAY_SIZE(regulator_devs);
  530. for (i = 0, j = -1; i < pdata->num_regulators; i++) {
  531. initdata = &pdata->regulator[i];
  532. if (strstr(initdata->constraints.name, "BUCK")) {
  533. sscanf(initdata->constraints.name, "BUCK%d", &j);
  534. /* BUCK1 ~ BUCK3 */
  535. if ((j < 1) || (j > 3)) {
  536. dev_err(chip->dev, "Failed to add constraint "
  537. "(%s)\n", initdata->constraints.name);
  538. goto out;
  539. }
  540. j = (j - 1) + PM8607_ID_BUCK1;
  541. }
  542. if (strstr(initdata->constraints.name, "LDO")) {
  543. sscanf(initdata->constraints.name, "LDO%d", &j);
  544. /* LDO1 ~ LDO15 */
  545. if ((j < 1) || (j > 15)) {
  546. dev_err(chip->dev, "Failed to add constraint "
  547. "(%s)\n", initdata->constraints.name);
  548. goto out;
  549. }
  550. j = (j - 1) + PM8607_ID_LDO1;
  551. }
  552. if (j == -1) {
  553. dev_err(chip->dev, "Failed to add constraint (%s)\n",
  554. initdata->constraints.name);
  555. goto out;
  556. }
  557. memcpy(&regulator_pdata[i], &pdata->regulator[i],
  558. sizeof(struct regulator_init_data));
  559. regulator_devs[i].mfd_data = &regulator_pdata[i];
  560. regulator_devs[i].num_resources = 1;
  561. regulator_devs[i].resources = &regulator_resources[j];
  562. ret = mfd_add_devices(chip->dev, 0, &regulator_devs[i], 1,
  563. &regulator_resources[j], 0);
  564. if (ret < 0) {
  565. dev_err(chip->dev, "Failed to add regulator subdev\n");
  566. goto out;
  567. }
  568. }
  569. out:
  570. return;
  571. }
  572. static void __devinit device_touch_init(struct pm860x_chip *chip,
  573. struct i2c_client *i2c,
  574. struct pm860x_platform_data *pdata)
  575. {
  576. int ret;
  577. if ((pdata == NULL) || (pdata->touch == NULL))
  578. return;
  579. memcpy(&touch_pdata, pdata->touch, sizeof(struct pm860x_touch_pdata));
  580. touch_devs[0].mfd_data = &touch_pdata;
  581. touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
  582. touch_devs[0].resources = &touch_resources[0];
  583. ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
  584. ARRAY_SIZE(touch_devs), &touch_resources[0],
  585. chip->irq_base);
  586. if (ret < 0)
  587. dev_err(chip->dev, "Failed to add touch subdev\n");
  588. }
  589. static void __devinit device_power_init(struct pm860x_chip *chip,
  590. struct i2c_client *i2c,
  591. struct pm860x_platform_data *pdata)
  592. {
  593. int ret;
  594. if ((pdata == NULL) || (pdata->power == NULL))
  595. return;
  596. memcpy(&power_pdata, pdata->power, sizeof(struct pm860x_power_pdata));
  597. power_devs[0].mfd_data = &power_pdata;
  598. power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
  599. power_devs[0].resources = &battery_resources[0],
  600. ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
  601. &battery_resources[0], chip->irq_base);
  602. if (ret < 0)
  603. dev_err(chip->dev, "Failed to add battery subdev\n");
  604. power_devs[1].mfd_data = &power_pdata;
  605. power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
  606. power_devs[1].resources = &charger_resources[0],
  607. ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
  608. &charger_resources[0], chip->irq_base);
  609. if (ret < 0)
  610. dev_err(chip->dev, "Failed to add charger subdev\n");
  611. }
  612. static void __devinit device_onkey_init(struct pm860x_chip *chip,
  613. struct i2c_client *i2c,
  614. struct pm860x_platform_data *pdata)
  615. {
  616. int ret;
  617. onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
  618. onkey_devs[0].resources = &onkey_resources[0],
  619. ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
  620. ARRAY_SIZE(onkey_devs), &onkey_resources[0],
  621. chip->irq_base);
  622. if (ret < 0)
  623. dev_err(chip->dev, "Failed to add onkey subdev\n");
  624. }
  625. static void __devinit device_codec_init(struct pm860x_chip *chip,
  626. struct i2c_client *i2c,
  627. struct pm860x_platform_data *pdata)
  628. {
  629. int ret;
  630. codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
  631. codec_devs[0].resources = &codec_resources[0],
  632. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  633. ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
  634. if (ret < 0)
  635. dev_err(chip->dev, "Failed to add codec subdev\n");
  636. }
  637. static void __devinit device_8607_init(struct pm860x_chip *chip,
  638. struct i2c_client *i2c,
  639. struct pm860x_platform_data *pdata)
  640. {
  641. int data, ret;
  642. ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
  643. if (ret < 0) {
  644. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  645. goto out;
  646. }
  647. switch (ret & PM8607_VERSION_MASK) {
  648. case 0x40:
  649. case 0x50:
  650. dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
  651. ret);
  652. break;
  653. default:
  654. dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
  655. "Chip ID: %02x\n", ret);
  656. goto out;
  657. }
  658. ret = pm860x_reg_read(i2c, PM8607_BUCK3);
  659. if (ret < 0) {
  660. dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
  661. goto out;
  662. }
  663. if (ret & PM8607_BUCK3_DOUBLE)
  664. chip->buck3_double = 1;
  665. ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
  666. if (ret < 0) {
  667. dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
  668. goto out;
  669. }
  670. if (pdata && (pdata->i2c_port == PI2C_PORT))
  671. data = PM8607_B0_MISC1_PI2C;
  672. else
  673. data = 0;
  674. ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
  675. if (ret < 0) {
  676. dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
  677. goto out;
  678. }
  679. ret = device_gpadc_init(chip, pdata);
  680. if (ret < 0)
  681. goto out;
  682. ret = device_irq_init(chip, pdata);
  683. if (ret < 0)
  684. goto out;
  685. device_regulator_init(chip, i2c, pdata);
  686. device_onkey_init(chip, i2c, pdata);
  687. device_touch_init(chip, i2c, pdata);
  688. device_power_init(chip, i2c, pdata);
  689. device_codec_init(chip, i2c, pdata);
  690. out:
  691. return;
  692. }
  693. int __devinit pm860x_device_init(struct pm860x_chip *chip,
  694. struct pm860x_platform_data *pdata)
  695. {
  696. chip->core_irq = 0;
  697. switch (chip->id) {
  698. case CHIP_PM8606:
  699. device_bk_init(chip, chip->client, pdata);
  700. device_led_init(chip, chip->client, pdata);
  701. break;
  702. case CHIP_PM8607:
  703. device_8607_init(chip, chip->client, pdata);
  704. break;
  705. }
  706. if (chip->companion) {
  707. switch (chip->id) {
  708. case CHIP_PM8607:
  709. device_bk_init(chip, chip->companion, pdata);
  710. device_led_init(chip, chip->companion, pdata);
  711. break;
  712. case CHIP_PM8606:
  713. device_8607_init(chip, chip->companion, pdata);
  714. break;
  715. }
  716. }
  717. return 0;
  718. }
  719. void __devexit pm860x_device_exit(struct pm860x_chip *chip)
  720. {
  721. device_irq_exit(chip);
  722. mfd_remove_devices(chip->dev);
  723. }
  724. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
  725. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  726. MODULE_LICENSE("GPL");