ab8500-core.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  6. * Author: Rabin Vincent <rabin.vincent@stericsson.com>
  7. * Author: Mattias Wallin <mattias.wallin@stericsson.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/mfd/abx500.h>
  19. #include <linux/mfd/abx500/ab8500.h>
  20. #include <linux/regulator/ab8500.h>
  21. /*
  22. * Interrupt register offsets
  23. * Bank : 0x0E
  24. */
  25. #define AB8500_IT_SOURCE1_REG 0x00
  26. #define AB8500_IT_SOURCE2_REG 0x01
  27. #define AB8500_IT_SOURCE3_REG 0x02
  28. #define AB8500_IT_SOURCE4_REG 0x03
  29. #define AB8500_IT_SOURCE5_REG 0x04
  30. #define AB8500_IT_SOURCE6_REG 0x05
  31. #define AB8500_IT_SOURCE7_REG 0x06
  32. #define AB8500_IT_SOURCE8_REG 0x07
  33. #define AB8500_IT_SOURCE19_REG 0x12
  34. #define AB8500_IT_SOURCE20_REG 0x13
  35. #define AB8500_IT_SOURCE21_REG 0x14
  36. #define AB8500_IT_SOURCE22_REG 0x15
  37. #define AB8500_IT_SOURCE23_REG 0x16
  38. #define AB8500_IT_SOURCE24_REG 0x17
  39. /*
  40. * latch registers
  41. */
  42. #define AB8500_IT_LATCH1_REG 0x20
  43. #define AB8500_IT_LATCH2_REG 0x21
  44. #define AB8500_IT_LATCH3_REG 0x22
  45. #define AB8500_IT_LATCH4_REG 0x23
  46. #define AB8500_IT_LATCH5_REG 0x24
  47. #define AB8500_IT_LATCH6_REG 0x25
  48. #define AB8500_IT_LATCH7_REG 0x26
  49. #define AB8500_IT_LATCH8_REG 0x27
  50. #define AB8500_IT_LATCH9_REG 0x28
  51. #define AB8500_IT_LATCH10_REG 0x29
  52. #define AB8500_IT_LATCH12_REG 0x2B
  53. #define AB8500_IT_LATCH19_REG 0x32
  54. #define AB8500_IT_LATCH20_REG 0x33
  55. #define AB8500_IT_LATCH21_REG 0x34
  56. #define AB8500_IT_LATCH22_REG 0x35
  57. #define AB8500_IT_LATCH23_REG 0x36
  58. #define AB8500_IT_LATCH24_REG 0x37
  59. /*
  60. * mask registers
  61. */
  62. #define AB8500_IT_MASK1_REG 0x40
  63. #define AB8500_IT_MASK2_REG 0x41
  64. #define AB8500_IT_MASK3_REG 0x42
  65. #define AB8500_IT_MASK4_REG 0x43
  66. #define AB8500_IT_MASK5_REG 0x44
  67. #define AB8500_IT_MASK6_REG 0x45
  68. #define AB8500_IT_MASK7_REG 0x46
  69. #define AB8500_IT_MASK8_REG 0x47
  70. #define AB8500_IT_MASK9_REG 0x48
  71. #define AB8500_IT_MASK10_REG 0x49
  72. #define AB8500_IT_MASK11_REG 0x4A
  73. #define AB8500_IT_MASK12_REG 0x4B
  74. #define AB8500_IT_MASK13_REG 0x4C
  75. #define AB8500_IT_MASK14_REG 0x4D
  76. #define AB8500_IT_MASK15_REG 0x4E
  77. #define AB8500_IT_MASK16_REG 0x4F
  78. #define AB8500_IT_MASK17_REG 0x50
  79. #define AB8500_IT_MASK18_REG 0x51
  80. #define AB8500_IT_MASK19_REG 0x52
  81. #define AB8500_IT_MASK20_REG 0x53
  82. #define AB8500_IT_MASK21_REG 0x54
  83. #define AB8500_IT_MASK22_REG 0x55
  84. #define AB8500_IT_MASK23_REG 0x56
  85. #define AB8500_IT_MASK24_REG 0x57
  86. #define AB8500_REV_REG 0x80
  87. #define AB8500_IC_NAME_REG 0x82
  88. #define AB8500_SWITCH_OFF_STATUS 0x00
  89. #define AB8500_TURN_ON_STATUS 0x00
  90. /*
  91. * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
  92. * numbers are indexed into this array with (num / 8). The interupts are
  93. * defined in linux/mfd/ab8500.h
  94. *
  95. * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
  96. * offset 0.
  97. */
  98. /* AB8500 support */
  99. static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
  100. 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
  101. };
  102. static const char ab8500_version_str[][7] = {
  103. [AB8500_VERSION_AB8500] = "AB8500",
  104. [AB8500_VERSION_AB8505] = "AB8505",
  105. [AB8500_VERSION_AB9540] = "AB9540",
  106. [AB8500_VERSION_AB8540] = "AB8540",
  107. };
  108. static int ab8500_get_chip_id(struct device *dev)
  109. {
  110. struct ab8500 *ab8500;
  111. if (!dev)
  112. return -EINVAL;
  113. ab8500 = dev_get_drvdata(dev->parent);
  114. return ab8500 ? (int)ab8500->chip_id : -EINVAL;
  115. }
  116. static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
  117. u8 reg, u8 data)
  118. {
  119. int ret;
  120. /*
  121. * Put the u8 bank and u8 register together into a an u16.
  122. * The bank on higher 8 bits and register in lower 8 bits.
  123. * */
  124. u16 addr = ((u16)bank) << 8 | reg;
  125. dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
  126. ret = mutex_lock_interruptible(&ab8500->lock);
  127. if (ret)
  128. return ret;
  129. ret = ab8500->write(ab8500, addr, data);
  130. if (ret < 0)
  131. dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
  132. addr, ret);
  133. mutex_unlock(&ab8500->lock);
  134. return ret;
  135. }
  136. static int ab8500_set_register(struct device *dev, u8 bank,
  137. u8 reg, u8 value)
  138. {
  139. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  140. return set_register_interruptible(ab8500, bank, reg, value);
  141. }
  142. static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
  143. u8 reg, u8 *value)
  144. {
  145. int ret;
  146. /* put the u8 bank and u8 reg together into a an u16.
  147. * bank on higher 8 bits and reg in lower */
  148. u16 addr = ((u16)bank) << 8 | reg;
  149. ret = mutex_lock_interruptible(&ab8500->lock);
  150. if (ret)
  151. return ret;
  152. ret = ab8500->read(ab8500, addr);
  153. if (ret < 0)
  154. dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
  155. addr, ret);
  156. else
  157. *value = ret;
  158. mutex_unlock(&ab8500->lock);
  159. dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
  160. return ret;
  161. }
  162. static int ab8500_get_register(struct device *dev, u8 bank,
  163. u8 reg, u8 *value)
  164. {
  165. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  166. return get_register_interruptible(ab8500, bank, reg, value);
  167. }
  168. static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
  169. u8 reg, u8 bitmask, u8 bitvalues)
  170. {
  171. int ret;
  172. u8 data;
  173. /* put the u8 bank and u8 reg together into a an u16.
  174. * bank on higher 8 bits and reg in lower */
  175. u16 addr = ((u16)bank) << 8 | reg;
  176. ret = mutex_lock_interruptible(&ab8500->lock);
  177. if (ret)
  178. return ret;
  179. ret = ab8500->read(ab8500, addr);
  180. if (ret < 0) {
  181. dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
  182. addr, ret);
  183. goto out;
  184. }
  185. data = (u8)ret;
  186. data = (~bitmask & data) | (bitmask & bitvalues);
  187. ret = ab8500->write(ab8500, addr, data);
  188. if (ret < 0)
  189. dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
  190. addr, ret);
  191. dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr, data);
  192. out:
  193. mutex_unlock(&ab8500->lock);
  194. return ret;
  195. }
  196. static int ab8500_mask_and_set_register(struct device *dev,
  197. u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
  198. {
  199. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  200. return mask_and_set_register_interruptible(ab8500, bank, reg,
  201. bitmask, bitvalues);
  202. }
  203. static struct abx500_ops ab8500_ops = {
  204. .get_chip_id = ab8500_get_chip_id,
  205. .get_register = ab8500_get_register,
  206. .set_register = ab8500_set_register,
  207. .get_register_page = NULL,
  208. .set_register_page = NULL,
  209. .mask_and_set_register = ab8500_mask_and_set_register,
  210. .event_registers_startup_state_get = NULL,
  211. .startup_irq_enabled = NULL,
  212. };
  213. static void ab8500_irq_lock(struct irq_data *data)
  214. {
  215. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  216. mutex_lock(&ab8500->irq_lock);
  217. }
  218. static void ab8500_irq_sync_unlock(struct irq_data *data)
  219. {
  220. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  221. int i;
  222. for (i = 0; i < ab8500->mask_size; i++) {
  223. u8 old = ab8500->oldmask[i];
  224. u8 new = ab8500->mask[i];
  225. int reg;
  226. if (new == old)
  227. continue;
  228. /*
  229. * Interrupt register 12 doesn't exist prior to AB8500 version
  230. * 2.0
  231. */
  232. if (ab8500->irq_reg_offset[i] == 11 &&
  233. is_ab8500_1p1_or_earlier(ab8500))
  234. continue;
  235. ab8500->oldmask[i] = new;
  236. reg = AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i];
  237. set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
  238. }
  239. mutex_unlock(&ab8500->irq_lock);
  240. }
  241. static void ab8500_irq_mask(struct irq_data *data)
  242. {
  243. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  244. int offset = data->irq - ab8500->irq_base;
  245. int index = offset / 8;
  246. int mask = 1 << (offset % 8);
  247. ab8500->mask[index] |= mask;
  248. }
  249. static void ab8500_irq_unmask(struct irq_data *data)
  250. {
  251. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  252. int offset = data->irq - ab8500->irq_base;
  253. int index = offset / 8;
  254. int mask = 1 << (offset % 8);
  255. ab8500->mask[index] &= ~mask;
  256. }
  257. static struct irq_chip ab8500_irq_chip = {
  258. .name = "ab8500",
  259. .irq_bus_lock = ab8500_irq_lock,
  260. .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
  261. .irq_mask = ab8500_irq_mask,
  262. .irq_disable = ab8500_irq_mask,
  263. .irq_unmask = ab8500_irq_unmask,
  264. };
  265. static irqreturn_t ab8500_irq(int irq, void *dev)
  266. {
  267. struct ab8500 *ab8500 = dev;
  268. int i;
  269. dev_vdbg(ab8500->dev, "interrupt\n");
  270. for (i = 0; i < ab8500->mask_size; i++) {
  271. int regoffset = ab8500->irq_reg_offset[i];
  272. int status;
  273. u8 value;
  274. /*
  275. * Interrupt register 12 doesn't exist prior to AB8500 version
  276. * 2.0
  277. */
  278. if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
  279. continue;
  280. status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
  281. AB8500_IT_LATCH1_REG + regoffset, &value);
  282. if (status < 0 || value == 0)
  283. continue;
  284. do {
  285. int bit = __ffs(value);
  286. int line = i * 8 + bit;
  287. handle_nested_irq(ab8500->irq_base + line);
  288. value &= ~(1 << bit);
  289. } while (value);
  290. }
  291. return IRQ_HANDLED;
  292. }
  293. static int ab8500_irq_init(struct ab8500 *ab8500)
  294. {
  295. int base = ab8500->irq_base;
  296. int irq;
  297. int num_irqs;
  298. num_irqs = AB8500_NR_IRQS;
  299. for (irq = base; irq < base + num_irqs; irq++) {
  300. irq_set_chip_data(irq, ab8500);
  301. irq_set_chip_and_handler(irq, &ab8500_irq_chip,
  302. handle_simple_irq);
  303. irq_set_nested_thread(irq, 1);
  304. #ifdef CONFIG_ARM
  305. set_irq_flags(irq, IRQF_VALID);
  306. #else
  307. irq_set_noprobe(irq);
  308. #endif
  309. }
  310. return 0;
  311. }
  312. static void ab8500_irq_remove(struct ab8500 *ab8500)
  313. {
  314. int base = ab8500->irq_base;
  315. int irq;
  316. int num_irqs;
  317. num_irqs = AB8500_NR_IRQS;
  318. for (irq = base; irq < base + num_irqs; irq++) {
  319. #ifdef CONFIG_ARM
  320. set_irq_flags(irq, 0);
  321. #endif
  322. irq_set_chip_and_handler(irq, NULL, NULL);
  323. irq_set_chip_data(irq, NULL);
  324. }
  325. }
  326. static struct resource __devinitdata ab8500_gpio_resources[] = {
  327. {
  328. .name = "GPIO_INT6",
  329. .start = AB8500_INT_GPIO6R,
  330. .end = AB8500_INT_GPIO41F,
  331. .flags = IORESOURCE_IRQ,
  332. }
  333. };
  334. static struct resource __devinitdata ab8500_gpadc_resources[] = {
  335. {
  336. .name = "HW_CONV_END",
  337. .start = AB8500_INT_GP_HW_ADC_CONV_END,
  338. .end = AB8500_INT_GP_HW_ADC_CONV_END,
  339. .flags = IORESOURCE_IRQ,
  340. },
  341. {
  342. .name = "SW_CONV_END",
  343. .start = AB8500_INT_GP_SW_ADC_CONV_END,
  344. .end = AB8500_INT_GP_SW_ADC_CONV_END,
  345. .flags = IORESOURCE_IRQ,
  346. },
  347. };
  348. static struct resource __devinitdata ab8500_rtc_resources[] = {
  349. {
  350. .name = "60S",
  351. .start = AB8500_INT_RTC_60S,
  352. .end = AB8500_INT_RTC_60S,
  353. .flags = IORESOURCE_IRQ,
  354. },
  355. {
  356. .name = "ALARM",
  357. .start = AB8500_INT_RTC_ALARM,
  358. .end = AB8500_INT_RTC_ALARM,
  359. .flags = IORESOURCE_IRQ,
  360. },
  361. };
  362. static struct resource __devinitdata ab8500_poweronkey_db_resources[] = {
  363. {
  364. .name = "ONKEY_DBF",
  365. .start = AB8500_INT_PON_KEY1DB_F,
  366. .end = AB8500_INT_PON_KEY1DB_F,
  367. .flags = IORESOURCE_IRQ,
  368. },
  369. {
  370. .name = "ONKEY_DBR",
  371. .start = AB8500_INT_PON_KEY1DB_R,
  372. .end = AB8500_INT_PON_KEY1DB_R,
  373. .flags = IORESOURCE_IRQ,
  374. },
  375. };
  376. static struct resource __devinitdata ab8500_av_acc_detect_resources[] = {
  377. {
  378. .name = "ACC_DETECT_1DB_F",
  379. .start = AB8500_INT_ACC_DETECT_1DB_F,
  380. .end = AB8500_INT_ACC_DETECT_1DB_F,
  381. .flags = IORESOURCE_IRQ,
  382. },
  383. {
  384. .name = "ACC_DETECT_1DB_R",
  385. .start = AB8500_INT_ACC_DETECT_1DB_R,
  386. .end = AB8500_INT_ACC_DETECT_1DB_R,
  387. .flags = IORESOURCE_IRQ,
  388. },
  389. {
  390. .name = "ACC_DETECT_21DB_F",
  391. .start = AB8500_INT_ACC_DETECT_21DB_F,
  392. .end = AB8500_INT_ACC_DETECT_21DB_F,
  393. .flags = IORESOURCE_IRQ,
  394. },
  395. {
  396. .name = "ACC_DETECT_21DB_R",
  397. .start = AB8500_INT_ACC_DETECT_21DB_R,
  398. .end = AB8500_INT_ACC_DETECT_21DB_R,
  399. .flags = IORESOURCE_IRQ,
  400. },
  401. {
  402. .name = "ACC_DETECT_22DB_F",
  403. .start = AB8500_INT_ACC_DETECT_22DB_F,
  404. .end = AB8500_INT_ACC_DETECT_22DB_F,
  405. .flags = IORESOURCE_IRQ,
  406. },
  407. {
  408. .name = "ACC_DETECT_22DB_R",
  409. .start = AB8500_INT_ACC_DETECT_22DB_R,
  410. .end = AB8500_INT_ACC_DETECT_22DB_R,
  411. .flags = IORESOURCE_IRQ,
  412. },
  413. };
  414. static struct resource __devinitdata ab8500_charger_resources[] = {
  415. {
  416. .name = "MAIN_CH_UNPLUG_DET",
  417. .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
  418. .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
  419. .flags = IORESOURCE_IRQ,
  420. },
  421. {
  422. .name = "MAIN_CHARGE_PLUG_DET",
  423. .start = AB8500_INT_MAIN_CH_PLUG_DET,
  424. .end = AB8500_INT_MAIN_CH_PLUG_DET,
  425. .flags = IORESOURCE_IRQ,
  426. },
  427. {
  428. .name = "VBUS_DET_R",
  429. .start = AB8500_INT_VBUS_DET_R,
  430. .end = AB8500_INT_VBUS_DET_R,
  431. .flags = IORESOURCE_IRQ,
  432. },
  433. {
  434. .name = "VBUS_DET_F",
  435. .start = AB8500_INT_VBUS_DET_F,
  436. .end = AB8500_INT_VBUS_DET_F,
  437. .flags = IORESOURCE_IRQ,
  438. },
  439. {
  440. .name = "USB_LINK_STATUS",
  441. .start = AB8500_INT_USB_LINK_STATUS,
  442. .end = AB8500_INT_USB_LINK_STATUS,
  443. .flags = IORESOURCE_IRQ,
  444. },
  445. {
  446. .name = "USB_CHARGE_DET_DONE",
  447. .start = AB8500_INT_USB_CHG_DET_DONE,
  448. .end = AB8500_INT_USB_CHG_DET_DONE,
  449. .flags = IORESOURCE_IRQ,
  450. },
  451. {
  452. .name = "VBUS_OVV",
  453. .start = AB8500_INT_VBUS_OVV,
  454. .end = AB8500_INT_VBUS_OVV,
  455. .flags = IORESOURCE_IRQ,
  456. },
  457. {
  458. .name = "USB_CH_TH_PROT_R",
  459. .start = AB8500_INT_USB_CH_TH_PROT_R,
  460. .end = AB8500_INT_USB_CH_TH_PROT_R,
  461. .flags = IORESOURCE_IRQ,
  462. },
  463. {
  464. .name = "USB_CH_TH_PROT_F",
  465. .start = AB8500_INT_USB_CH_TH_PROT_F,
  466. .end = AB8500_INT_USB_CH_TH_PROT_F,
  467. .flags = IORESOURCE_IRQ,
  468. },
  469. {
  470. .name = "MAIN_EXT_CH_NOT_OK",
  471. .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  472. .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  473. .flags = IORESOURCE_IRQ,
  474. },
  475. {
  476. .name = "MAIN_CH_TH_PROT_R",
  477. .start = AB8500_INT_MAIN_CH_TH_PROT_R,
  478. .end = AB8500_INT_MAIN_CH_TH_PROT_R,
  479. .flags = IORESOURCE_IRQ,
  480. },
  481. {
  482. .name = "MAIN_CH_TH_PROT_F",
  483. .start = AB8500_INT_MAIN_CH_TH_PROT_F,
  484. .end = AB8500_INT_MAIN_CH_TH_PROT_F,
  485. .flags = IORESOURCE_IRQ,
  486. },
  487. {
  488. .name = "USB_CHARGER_NOT_OKR",
  489. .start = AB8500_INT_USB_CHARGER_NOT_OK,
  490. .end = AB8500_INT_USB_CHARGER_NOT_OK,
  491. .flags = IORESOURCE_IRQ,
  492. },
  493. {
  494. .name = "USB_CHARGER_NOT_OKF",
  495. .start = AB8500_INT_USB_CHARGER_NOT_OKF,
  496. .end = AB8500_INT_USB_CHARGER_NOT_OKF,
  497. .flags = IORESOURCE_IRQ,
  498. },
  499. {
  500. .name = "CH_WD_EXP",
  501. .start = AB8500_INT_CH_WD_EXP,
  502. .end = AB8500_INT_CH_WD_EXP,
  503. .flags = IORESOURCE_IRQ,
  504. },
  505. };
  506. static struct resource __devinitdata ab8500_btemp_resources[] = {
  507. {
  508. .name = "BAT_CTRL_INDB",
  509. .start = AB8500_INT_BAT_CTRL_INDB,
  510. .end = AB8500_INT_BAT_CTRL_INDB,
  511. .flags = IORESOURCE_IRQ,
  512. },
  513. {
  514. .name = "BTEMP_LOW",
  515. .start = AB8500_INT_BTEMP_LOW,
  516. .end = AB8500_INT_BTEMP_LOW,
  517. .flags = IORESOURCE_IRQ,
  518. },
  519. {
  520. .name = "BTEMP_HIGH",
  521. .start = AB8500_INT_BTEMP_HIGH,
  522. .end = AB8500_INT_BTEMP_HIGH,
  523. .flags = IORESOURCE_IRQ,
  524. },
  525. {
  526. .name = "BTEMP_LOW_MEDIUM",
  527. .start = AB8500_INT_BTEMP_LOW_MEDIUM,
  528. .end = AB8500_INT_BTEMP_LOW_MEDIUM,
  529. .flags = IORESOURCE_IRQ,
  530. },
  531. {
  532. .name = "BTEMP_MEDIUM_HIGH",
  533. .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
  534. .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
  535. .flags = IORESOURCE_IRQ,
  536. },
  537. };
  538. static struct resource __devinitdata ab8500_fg_resources[] = {
  539. {
  540. .name = "NCONV_ACCU",
  541. .start = AB8500_INT_CCN_CONV_ACC,
  542. .end = AB8500_INT_CCN_CONV_ACC,
  543. .flags = IORESOURCE_IRQ,
  544. },
  545. {
  546. .name = "BATT_OVV",
  547. .start = AB8500_INT_BATT_OVV,
  548. .end = AB8500_INT_BATT_OVV,
  549. .flags = IORESOURCE_IRQ,
  550. },
  551. {
  552. .name = "LOW_BAT_F",
  553. .start = AB8500_INT_LOW_BAT_F,
  554. .end = AB8500_INT_LOW_BAT_F,
  555. .flags = IORESOURCE_IRQ,
  556. },
  557. {
  558. .name = "LOW_BAT_R",
  559. .start = AB8500_INT_LOW_BAT_R,
  560. .end = AB8500_INT_LOW_BAT_R,
  561. .flags = IORESOURCE_IRQ,
  562. },
  563. {
  564. .name = "CC_INT_CALIB",
  565. .start = AB8500_INT_CC_INT_CALIB,
  566. .end = AB8500_INT_CC_INT_CALIB,
  567. .flags = IORESOURCE_IRQ,
  568. },
  569. };
  570. static struct resource __devinitdata ab8500_chargalg_resources[] = {};
  571. #ifdef CONFIG_DEBUG_FS
  572. static struct resource __devinitdata ab8500_debug_resources[] = {
  573. {
  574. .name = "IRQ_FIRST",
  575. .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  576. .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  577. .flags = IORESOURCE_IRQ,
  578. },
  579. {
  580. .name = "IRQ_LAST",
  581. .start = AB8500_INT_USB_CHARGER_NOT_OKF,
  582. .end = AB8500_INT_USB_CHARGER_NOT_OKF,
  583. .flags = IORESOURCE_IRQ,
  584. },
  585. };
  586. #endif
  587. static struct resource __devinitdata ab8500_usb_resources[] = {
  588. {
  589. .name = "ID_WAKEUP_R",
  590. .start = AB8500_INT_ID_WAKEUP_R,
  591. .end = AB8500_INT_ID_WAKEUP_R,
  592. .flags = IORESOURCE_IRQ,
  593. },
  594. {
  595. .name = "ID_WAKEUP_F",
  596. .start = AB8500_INT_ID_WAKEUP_F,
  597. .end = AB8500_INT_ID_WAKEUP_F,
  598. .flags = IORESOURCE_IRQ,
  599. },
  600. {
  601. .name = "VBUS_DET_F",
  602. .start = AB8500_INT_VBUS_DET_F,
  603. .end = AB8500_INT_VBUS_DET_F,
  604. .flags = IORESOURCE_IRQ,
  605. },
  606. {
  607. .name = "VBUS_DET_R",
  608. .start = AB8500_INT_VBUS_DET_R,
  609. .end = AB8500_INT_VBUS_DET_R,
  610. .flags = IORESOURCE_IRQ,
  611. },
  612. {
  613. .name = "USB_LINK_STATUS",
  614. .start = AB8500_INT_USB_LINK_STATUS,
  615. .end = AB8500_INT_USB_LINK_STATUS,
  616. .flags = IORESOURCE_IRQ,
  617. },
  618. {
  619. .name = "USB_ADP_PROBE_PLUG",
  620. .start = AB8500_INT_ADP_PROBE_PLUG,
  621. .end = AB8500_INT_ADP_PROBE_PLUG,
  622. .flags = IORESOURCE_IRQ,
  623. },
  624. {
  625. .name = "USB_ADP_PROBE_UNPLUG",
  626. .start = AB8500_INT_ADP_PROBE_UNPLUG,
  627. .end = AB8500_INT_ADP_PROBE_UNPLUG,
  628. .flags = IORESOURCE_IRQ,
  629. },
  630. };
  631. static struct resource __devinitdata ab8500_temp_resources[] = {
  632. {
  633. .name = "AB8500_TEMP_WARM",
  634. .start = AB8500_INT_TEMP_WARM,
  635. .end = AB8500_INT_TEMP_WARM,
  636. .flags = IORESOURCE_IRQ,
  637. },
  638. };
  639. static struct mfd_cell __devinitdata ab8500_devs[] = {
  640. #ifdef CONFIG_DEBUG_FS
  641. {
  642. .name = "ab8500-debug",
  643. .num_resources = ARRAY_SIZE(ab8500_debug_resources),
  644. .resources = ab8500_debug_resources,
  645. },
  646. #endif
  647. {
  648. .name = "ab8500-sysctrl",
  649. },
  650. {
  651. .name = "ab8500-regulator",
  652. },
  653. {
  654. .name = "ab8500-gpio",
  655. .num_resources = ARRAY_SIZE(ab8500_gpio_resources),
  656. .resources = ab8500_gpio_resources,
  657. },
  658. {
  659. .name = "ab8500-gpadc",
  660. .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
  661. .resources = ab8500_gpadc_resources,
  662. },
  663. {
  664. .name = "ab8500-rtc",
  665. .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
  666. .resources = ab8500_rtc_resources,
  667. },
  668. {
  669. .name = "ab8500-charger",
  670. .num_resources = ARRAY_SIZE(ab8500_charger_resources),
  671. .resources = ab8500_charger_resources,
  672. },
  673. {
  674. .name = "ab8500-btemp",
  675. .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
  676. .resources = ab8500_btemp_resources,
  677. },
  678. {
  679. .name = "ab8500-fg",
  680. .num_resources = ARRAY_SIZE(ab8500_fg_resources),
  681. .resources = ab8500_fg_resources,
  682. },
  683. {
  684. .name = "ab8500-chargalg",
  685. .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
  686. .resources = ab8500_chargalg_resources,
  687. },
  688. {
  689. .name = "ab8500-acc-det",
  690. .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
  691. .resources = ab8500_av_acc_detect_resources,
  692. },
  693. {
  694. .name = "ab8500-codec",
  695. },
  696. {
  697. .name = "ab8500-usb",
  698. .num_resources = ARRAY_SIZE(ab8500_usb_resources),
  699. .resources = ab8500_usb_resources,
  700. },
  701. {
  702. .name = "ab8500-poweron-key",
  703. .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
  704. .resources = ab8500_poweronkey_db_resources,
  705. },
  706. {
  707. .name = "ab8500-pwm",
  708. .id = 1,
  709. },
  710. {
  711. .name = "ab8500-pwm",
  712. .id = 2,
  713. },
  714. {
  715. .name = "ab8500-pwm",
  716. .id = 3,
  717. },
  718. { .name = "ab8500-leds", },
  719. {
  720. .name = "ab8500-denc",
  721. },
  722. {
  723. .name = "ab8500-temp",
  724. .num_resources = ARRAY_SIZE(ab8500_temp_resources),
  725. .resources = ab8500_temp_resources,
  726. },
  727. };
  728. static ssize_t show_chip_id(struct device *dev,
  729. struct device_attribute *attr, char *buf)
  730. {
  731. struct ab8500 *ab8500;
  732. ab8500 = dev_get_drvdata(dev);
  733. return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
  734. }
  735. /*
  736. * ab8500 has switched off due to (SWITCH_OFF_STATUS):
  737. * 0x01 Swoff bit programming
  738. * 0x02 Thermal protection activation
  739. * 0x04 Vbat lower then BattOk falling threshold
  740. * 0x08 Watchdog expired
  741. * 0x10 Non presence of 32kHz clock
  742. * 0x20 Battery level lower than power on reset threshold
  743. * 0x40 Power on key 1 pressed longer than 10 seconds
  744. * 0x80 DB8500 thermal shutdown
  745. */
  746. static ssize_t show_switch_off_status(struct device *dev,
  747. struct device_attribute *attr, char *buf)
  748. {
  749. int ret;
  750. u8 value;
  751. struct ab8500 *ab8500;
  752. ab8500 = dev_get_drvdata(dev);
  753. ret = get_register_interruptible(ab8500, AB8500_RTC,
  754. AB8500_SWITCH_OFF_STATUS, &value);
  755. if (ret < 0)
  756. return ret;
  757. return sprintf(buf, "%#x\n", value);
  758. }
  759. /*
  760. * ab8500 has turned on due to (TURN_ON_STATUS):
  761. * 0x01 PORnVbat
  762. * 0x02 PonKey1dbF
  763. * 0x04 PonKey2dbF
  764. * 0x08 RTCAlarm
  765. * 0x10 MainChDet
  766. * 0x20 VbusDet
  767. * 0x40 UsbIDDetect
  768. * 0x80 Reserved
  769. */
  770. static ssize_t show_turn_on_status(struct device *dev,
  771. struct device_attribute *attr, char *buf)
  772. {
  773. int ret;
  774. u8 value;
  775. struct ab8500 *ab8500;
  776. ab8500 = dev_get_drvdata(dev);
  777. ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
  778. AB8500_TURN_ON_STATUS, &value);
  779. if (ret < 0)
  780. return ret;
  781. return sprintf(buf, "%#x\n", value);
  782. }
  783. static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
  784. static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
  785. static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
  786. static struct attribute *ab8500_sysfs_entries[] = {
  787. &dev_attr_chip_id.attr,
  788. &dev_attr_switch_off_status.attr,
  789. &dev_attr_turn_on_status.attr,
  790. NULL,
  791. };
  792. static struct attribute_group ab8500_attr_group = {
  793. .attrs = ab8500_sysfs_entries,
  794. };
  795. int __devinit ab8500_init(struct ab8500 *ab8500, enum ab8500_version version)
  796. {
  797. struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
  798. int ret;
  799. int i;
  800. u8 value;
  801. if (plat)
  802. ab8500->irq_base = plat->irq_base;
  803. mutex_init(&ab8500->lock);
  804. mutex_init(&ab8500->irq_lock);
  805. if (version != AB8500_VERSION_UNDEFINED)
  806. ab8500->version = version;
  807. else {
  808. ret = get_register_interruptible(ab8500, AB8500_MISC,
  809. AB8500_IC_NAME_REG, &value);
  810. if (ret < 0)
  811. return ret;
  812. ab8500->version = value;
  813. }
  814. ret = get_register_interruptible(ab8500, AB8500_MISC,
  815. AB8500_REV_REG, &value);
  816. if (ret < 0)
  817. return ret;
  818. ab8500->chip_id = value;
  819. dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
  820. ab8500_version_str[ab8500->version],
  821. ab8500->chip_id >> 4,
  822. ab8500->chip_id & 0x0F);
  823. ab8500->mask_size = AB8500_NUM_IRQ_REGS;
  824. ab8500->irq_reg_offset = ab8500_irq_regoffset;
  825. ab8500->mask = kzalloc(ab8500->mask_size, GFP_KERNEL);
  826. if (!ab8500->mask)
  827. return -ENOMEM;
  828. ab8500->oldmask = kzalloc(ab8500->mask_size, GFP_KERNEL);
  829. if (!ab8500->oldmask) {
  830. ret = -ENOMEM;
  831. goto out_freemask;
  832. }
  833. /*
  834. * ab8500 has switched off due to (SWITCH_OFF_STATUS):
  835. * 0x01 Swoff bit programming
  836. * 0x02 Thermal protection activation
  837. * 0x04 Vbat lower then BattOk falling threshold
  838. * 0x08 Watchdog expired
  839. * 0x10 Non presence of 32kHz clock
  840. * 0x20 Battery level lower than power on reset threshold
  841. * 0x40 Power on key 1 pressed longer than 10 seconds
  842. * 0x80 DB8500 thermal shutdown
  843. */
  844. ret = get_register_interruptible(ab8500, AB8500_RTC,
  845. AB8500_SWITCH_OFF_STATUS, &value);
  846. if (ret < 0)
  847. return ret;
  848. dev_info(ab8500->dev, "switch off status: %#x", value);
  849. if (plat && plat->init)
  850. plat->init(ab8500);
  851. /* Clear and mask all interrupts */
  852. for (i = 0; i < ab8500->mask_size; i++) {
  853. /*
  854. * Interrupt register 12 doesn't exist prior to AB8500 version
  855. * 2.0
  856. */
  857. if (ab8500->irq_reg_offset[i] == 11 &&
  858. is_ab8500_1p1_or_earlier(ab8500))
  859. continue;
  860. get_register_interruptible(ab8500, AB8500_INTERRUPT,
  861. AB8500_IT_LATCH1_REG + ab8500->irq_reg_offset[i],
  862. &value);
  863. set_register_interruptible(ab8500, AB8500_INTERRUPT,
  864. AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i], 0xff);
  865. }
  866. ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
  867. if (ret)
  868. goto out_freeoldmask;
  869. for (i = 0; i < ab8500->mask_size; i++)
  870. ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
  871. if (ab8500->irq_base) {
  872. ret = ab8500_irq_init(ab8500);
  873. if (ret)
  874. goto out_freeoldmask;
  875. ret = request_threaded_irq(ab8500->irq, NULL, ab8500_irq,
  876. IRQF_ONESHOT | IRQF_NO_SUSPEND,
  877. "ab8500", ab8500);
  878. if (ret)
  879. goto out_removeirq;
  880. }
  881. ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
  882. ARRAY_SIZE(ab8500_devs), NULL,
  883. ab8500->irq_base);
  884. if (ret)
  885. goto out_freeirq;
  886. ret = sysfs_create_group(&ab8500->dev->kobj, &ab8500_attr_group);
  887. if (ret)
  888. dev_err(ab8500->dev, "error creating sysfs entries\n");
  889. return ret;
  890. out_freeirq:
  891. if (ab8500->irq_base)
  892. free_irq(ab8500->irq, ab8500);
  893. out_removeirq:
  894. if (ab8500->irq_base)
  895. ab8500_irq_remove(ab8500);
  896. out_freeoldmask:
  897. kfree(ab8500->oldmask);
  898. out_freemask:
  899. kfree(ab8500->mask);
  900. return ret;
  901. }
  902. int __devexit ab8500_exit(struct ab8500 *ab8500)
  903. {
  904. sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
  905. mfd_remove_devices(ab8500->dev);
  906. if (ab8500->irq_base) {
  907. free_irq(ab8500->irq, ab8500);
  908. ab8500_irq_remove(ab8500);
  909. }
  910. kfree(ab8500->oldmask);
  911. kfree(ab8500->mask);
  912. return 0;
  913. }
  914. MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
  915. MODULE_DESCRIPTION("AB8500 MFD core");
  916. MODULE_LICENSE("GPL v2");