twl4030-core.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
  3. *
  4. * Copyright (C) 2005-2006 Texas Instruments, Inc.
  5. *
  6. * Modifications to defer interrupt handling to a kernel thread:
  7. * Copyright (C) 2006 MontaVista Software, Inc.
  8. *
  9. * Based on tlv320aic23.c:
  10. * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
  11. *
  12. * Code cleanup and modifications to IRQ handler.
  13. * by syed khasim <x0khasim@ti.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/kernel_stat.h>
  30. #include <linux/init.h>
  31. #include <linux/mutex.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/irq.h>
  34. #include <linux/random.h>
  35. #include <linux/kthread.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/clk.h>
  38. #include <linux/i2c.h>
  39. #include <linux/i2c/twl4030.h>
  40. /*
  41. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  42. * Management and System Companion Device" chips originally designed for
  43. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  44. * often at around 3 Mbit/sec, including for interrupt handling.
  45. *
  46. * This driver core provides genirq support for the interrupts emitted,
  47. * by the various modules, and exports register access primitives.
  48. *
  49. * FIXME this driver currently requires use of the first interrupt line
  50. * (and associated registers).
  51. */
  52. #define DRIVER_NAME "twl4030"
  53. #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
  54. defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
  55. #define twl_has_bci() true
  56. #else
  57. #define twl_has_bci() false
  58. #endif
  59. #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
  60. #define twl_has_keypad() true
  61. #else
  62. #define twl_has_keypad() false
  63. #endif
  64. #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
  65. #define twl_has_gpio() true
  66. #else
  67. #define twl_has_gpio() false
  68. #endif
  69. #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
  70. #define twl_has_madc() true
  71. #else
  72. #define twl_has_madc() false
  73. #endif
  74. #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
  75. #define twl_has_rtc() true
  76. #else
  77. #define twl_has_rtc() false
  78. #endif
  79. #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
  80. #define twl_has_usb() true
  81. #else
  82. #define twl_has_usb() false
  83. #endif
  84. static inline void activate_irq(int irq)
  85. {
  86. #ifdef CONFIG_ARM
  87. /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
  88. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  89. */
  90. set_irq_flags(irq, IRQF_VALID);
  91. #else
  92. /* same effect on other architectures */
  93. set_irq_noprobe(irq);
  94. #endif
  95. }
  96. /* Primary Interrupt Handler on TWL4030 Registers */
  97. /* Register Definitions */
  98. #define REG_PIH_ISR_P1 (0x1)
  99. #define REG_PIH_ISR_P2 (0x2)
  100. #define REG_PIH_SIR (0x3)
  101. /* Triton Core internal information (BEGIN) */
  102. /* Last - for index max*/
  103. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  104. #define TWL4030_NUM_SLAVES 4
  105. /* Base Address defns for twl4030_map[] */
  106. /* subchip/slave 0 - USB ID */
  107. #define TWL4030_BASEADD_USB 0x0000
  108. /* subchip/slave 1 - AUD ID */
  109. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  110. #define TWL4030_BASEADD_GPIO 0x0098
  111. #define TWL4030_BASEADD_INTBR 0x0085
  112. #define TWL4030_BASEADD_PIH 0x0080
  113. #define TWL4030_BASEADD_TEST 0x004C
  114. /* subchip/slave 2 - AUX ID */
  115. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  116. #define TWL4030_BASEADD_LED 0x00EE
  117. #define TWL4030_BASEADD_MADC 0x0000
  118. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  119. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  120. #define TWL4030_BASEADD_PWM0 0x00F8
  121. #define TWL4030_BASEADD_PWM1 0x00FB
  122. #define TWL4030_BASEADD_PWMA 0x00EF
  123. #define TWL4030_BASEADD_PWMB 0x00F1
  124. #define TWL4030_BASEADD_KEYPAD 0x00D2
  125. /* subchip/slave 3 - POWER ID */
  126. #define TWL4030_BASEADD_BACKUP 0x0014
  127. #define TWL4030_BASEADD_INT 0x002E
  128. #define TWL4030_BASEADD_PM_MASTER 0x0036
  129. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  130. #define TWL4030_BASEADD_RTC 0x001C
  131. #define TWL4030_BASEADD_SECURED_REG 0x0000
  132. /* Triton Core internal information (END) */
  133. /* Few power values */
  134. #define R_CFG_BOOT 0x05
  135. #define R_PROTECT_KEY 0x0E
  136. /* access control values for R_PROTECT_KEY */
  137. #define KEY_UNLOCK1 0xce
  138. #define KEY_UNLOCK2 0xec
  139. #define KEY_LOCK 0x00
  140. /* some fields in R_CFG_BOOT */
  141. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  142. #define HFCLK_FREQ_26_MHZ (2 << 0)
  143. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  144. #define HIGH_PERF_SQ (1 << 3)
  145. /*----------------------------------------------------------------------*/
  146. /**
  147. * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
  148. * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
  149. * @sih_ctrl: address of module SIH_CTRL register
  150. * @reg_cnt: number of IMR/ISR regs
  151. * @imrs: pointer to array of TWL module interrupt mask register indices
  152. * @isrs: pointer to array of TWL module interrupt status register indices
  153. *
  154. * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
  155. * during twl_init_irq().
  156. */
  157. struct twl4030_mod_iregs {
  158. const u8 mod_no;
  159. const u8 sih_ctrl;
  160. const u8 reg_cnt;
  161. const u8 *imrs;
  162. const u8 *isrs;
  163. };
  164. /* TWL4030 INT module interrupt mask registers */
  165. static const u8 __initconst twl4030_int_imr_regs[] = {
  166. TWL4030_INT_PWR_IMR1,
  167. TWL4030_INT_PWR_IMR2,
  168. };
  169. /* TWL4030 INT module interrupt status registers */
  170. static const u8 __initconst twl4030_int_isr_regs[] = {
  171. TWL4030_INT_PWR_ISR1,
  172. TWL4030_INT_PWR_ISR2,
  173. };
  174. /* TWL4030 INTERRUPTS module interrupt mask registers */
  175. static const u8 __initconst twl4030_interrupts_imr_regs[] = {
  176. TWL4030_INTERRUPTS_BCIIMR1A,
  177. TWL4030_INTERRUPTS_BCIIMR1B,
  178. TWL4030_INTERRUPTS_BCIIMR2A,
  179. TWL4030_INTERRUPTS_BCIIMR2B,
  180. };
  181. /* TWL4030 INTERRUPTS module interrupt status registers */
  182. static const u8 __initconst twl4030_interrupts_isr_regs[] = {
  183. TWL4030_INTERRUPTS_BCIISR1A,
  184. TWL4030_INTERRUPTS_BCIISR1B,
  185. TWL4030_INTERRUPTS_BCIISR2A,
  186. TWL4030_INTERRUPTS_BCIISR2B,
  187. };
  188. /* TWL4030 MADC module interrupt mask registers */
  189. static const u8 __initconst twl4030_madc_imr_regs[] = {
  190. TWL4030_MADC_IMR1,
  191. TWL4030_MADC_IMR2,
  192. };
  193. /* TWL4030 MADC module interrupt status registers */
  194. static const u8 __initconst twl4030_madc_isr_regs[] = {
  195. TWL4030_MADC_ISR1,
  196. TWL4030_MADC_ISR2,
  197. };
  198. /* TWL4030 keypad module interrupt mask registers */
  199. static const u8 __initconst twl4030_keypad_imr_regs[] = {
  200. TWL4030_KEYPAD_KEYP_IMR1,
  201. TWL4030_KEYPAD_KEYP_IMR2,
  202. };
  203. /* TWL4030 keypad module interrupt status registers */
  204. static const u8 __initconst twl4030_keypad_isr_regs[] = {
  205. TWL4030_KEYPAD_KEYP_ISR1,
  206. TWL4030_KEYPAD_KEYP_ISR2,
  207. };
  208. /* TWL4030 GPIO module interrupt mask registers */
  209. static const u8 __initconst twl4030_gpio_imr_regs[] = {
  210. REG_GPIO_IMR1A,
  211. REG_GPIO_IMR1B,
  212. REG_GPIO_IMR2A,
  213. REG_GPIO_IMR2B,
  214. REG_GPIO_IMR3A,
  215. REG_GPIO_IMR3B,
  216. };
  217. /* TWL4030 GPIO module interrupt status registers */
  218. static const u8 __initconst twl4030_gpio_isr_regs[] = {
  219. REG_GPIO_ISR1A,
  220. REG_GPIO_ISR1B,
  221. REG_GPIO_ISR2A,
  222. REG_GPIO_ISR2B,
  223. REG_GPIO_ISR3A,
  224. REG_GPIO_ISR3B,
  225. };
  226. /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
  227. static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
  228. {
  229. .mod_no = TWL4030_MODULE_INT,
  230. .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
  231. .reg_cnt = ARRAY_SIZE(twl4030_int_imr_regs),
  232. .imrs = twl4030_int_imr_regs,
  233. .isrs = twl4030_int_isr_regs,
  234. },
  235. {
  236. .mod_no = TWL4030_MODULE_INTERRUPTS,
  237. .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
  238. .reg_cnt = ARRAY_SIZE(twl4030_interrupts_imr_regs),
  239. .imrs = twl4030_interrupts_imr_regs,
  240. .isrs = twl4030_interrupts_isr_regs,
  241. },
  242. {
  243. .mod_no = TWL4030_MODULE_MADC,
  244. .sih_ctrl = TWL4030_MADC_SIH_CTRL,
  245. .reg_cnt = ARRAY_SIZE(twl4030_madc_imr_regs),
  246. .imrs = twl4030_madc_imr_regs,
  247. .isrs = twl4030_madc_isr_regs,
  248. },
  249. {
  250. .mod_no = TWL4030_MODULE_KEYPAD,
  251. .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
  252. .reg_cnt = ARRAY_SIZE(twl4030_keypad_imr_regs),
  253. .imrs = twl4030_keypad_imr_regs,
  254. .isrs = twl4030_keypad_isr_regs,
  255. },
  256. {
  257. .mod_no = TWL4030_MODULE_GPIO,
  258. .sih_ctrl = REG_GPIO_SIH_CTRL,
  259. .reg_cnt = ARRAY_SIZE(twl4030_gpio_imr_regs),
  260. .imrs = twl4030_gpio_imr_regs,
  261. .isrs = twl4030_gpio_isr_regs,
  262. },
  263. };
  264. /*----------------------------------------------------------------*/
  265. /* is driver active, bound to a chip? */
  266. static bool inuse;
  267. /* Structure for each TWL4030 Slave */
  268. struct twl4030_client {
  269. struct i2c_client *client;
  270. u8 address;
  271. /* max numb of i2c_msg required is for read =2 */
  272. struct i2c_msg xfer_msg[2];
  273. /* To lock access to xfer_msg */
  274. struct mutex xfer_lock;
  275. };
  276. static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
  277. /* mapping the module id to slave id and base address */
  278. struct twl4030mapping {
  279. unsigned char sid; /* Slave ID */
  280. unsigned char base; /* base address */
  281. };
  282. static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  283. /*
  284. * NOTE: don't change this table without updating the
  285. * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
  286. * so they continue to match the order in this table.
  287. */
  288. { 0, TWL4030_BASEADD_USB },
  289. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  290. { 1, TWL4030_BASEADD_GPIO },
  291. { 1, TWL4030_BASEADD_INTBR },
  292. { 1, TWL4030_BASEADD_PIH },
  293. { 1, TWL4030_BASEADD_TEST },
  294. { 2, TWL4030_BASEADD_KEYPAD },
  295. { 2, TWL4030_BASEADD_MADC },
  296. { 2, TWL4030_BASEADD_INTERRUPTS },
  297. { 2, TWL4030_BASEADD_LED },
  298. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  299. { 2, TWL4030_BASEADD_PRECHARGE },
  300. { 2, TWL4030_BASEADD_PWM0 },
  301. { 2, TWL4030_BASEADD_PWM1 },
  302. { 2, TWL4030_BASEADD_PWMA },
  303. { 2, TWL4030_BASEADD_PWMB },
  304. { 3, TWL4030_BASEADD_BACKUP },
  305. { 3, TWL4030_BASEADD_INT },
  306. { 3, TWL4030_BASEADD_PM_MASTER },
  307. { 3, TWL4030_BASEADD_PM_RECEIVER },
  308. { 3, TWL4030_BASEADD_RTC },
  309. { 3, TWL4030_BASEADD_SECURED_REG },
  310. };
  311. /*----------------------------------------------------------------------*/
  312. /*
  313. * TWL4030 doesn't have PIH mask, hence dummy function for mask
  314. * and unmask of the (eight) interrupts reported at that level ...
  315. * masking is only available from SIH (secondary) modules.
  316. */
  317. static void twl4030_i2c_ackirq(unsigned int irq)
  318. {
  319. }
  320. static void twl4030_i2c_disableint(unsigned int irq)
  321. {
  322. }
  323. static void twl4030_i2c_enableint(unsigned int irq)
  324. {
  325. }
  326. static struct irq_chip twl4030_irq_chip = {
  327. .name = "twl4030",
  328. .ack = twl4030_i2c_ackirq,
  329. .mask = twl4030_i2c_disableint,
  330. .unmask = twl4030_i2c_enableint,
  331. };
  332. /*----------------------------------------------------------------------*/
  333. /* Exported Functions */
  334. /**
  335. * twl4030_i2c_write - Writes a n bit register in TWL4030
  336. * @mod_no: module number
  337. * @value: an array of num_bytes+1 containing data to write
  338. * @reg: register address (just offset will do)
  339. * @num_bytes: number of bytes to transfer
  340. *
  341. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  342. * valid data starts at Offset 1.
  343. *
  344. * Returns the result of operation - 0 is success
  345. */
  346. int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
  347. {
  348. int ret;
  349. int sid;
  350. struct twl4030_client *twl;
  351. struct i2c_msg *msg;
  352. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  353. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  354. return -EPERM;
  355. }
  356. sid = twl4030_map[mod_no].sid;
  357. twl = &twl4030_modules[sid];
  358. if (unlikely(!inuse)) {
  359. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  360. return -EPERM;
  361. }
  362. mutex_lock(&twl->xfer_lock);
  363. /*
  364. * [MSG1]: fill the register address data
  365. * fill the data Tx buffer
  366. */
  367. msg = &twl->xfer_msg[0];
  368. msg->addr = twl->address;
  369. msg->len = num_bytes + 1;
  370. msg->flags = 0;
  371. msg->buf = value;
  372. /* over write the first byte of buffer with the register address */
  373. *value = twl4030_map[mod_no].base + reg;
  374. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  375. mutex_unlock(&twl->xfer_lock);
  376. /* i2cTransfer returns num messages.translate it pls.. */
  377. if (ret >= 0)
  378. ret = 0;
  379. return ret;
  380. }
  381. EXPORT_SYMBOL(twl4030_i2c_write);
  382. /**
  383. * twl4030_i2c_read - Reads a n bit register in TWL4030
  384. * @mod_no: module number
  385. * @value: an array of num_bytes containing data to be read
  386. * @reg: register address (just offset will do)
  387. * @num_bytes: number of bytes to transfer
  388. *
  389. * Returns result of operation - num_bytes is success else failure.
  390. */
  391. int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
  392. {
  393. int ret;
  394. u8 val;
  395. int sid;
  396. struct twl4030_client *twl;
  397. struct i2c_msg *msg;
  398. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  399. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  400. return -EPERM;
  401. }
  402. sid = twl4030_map[mod_no].sid;
  403. twl = &twl4030_modules[sid];
  404. if (unlikely(!inuse)) {
  405. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  406. return -EPERM;
  407. }
  408. mutex_lock(&twl->xfer_lock);
  409. /* [MSG1] fill the register address data */
  410. msg = &twl->xfer_msg[0];
  411. msg->addr = twl->address;
  412. msg->len = 1;
  413. msg->flags = 0; /* Read the register value */
  414. val = twl4030_map[mod_no].base + reg;
  415. msg->buf = &val;
  416. /* [MSG2] fill the data rx buffer */
  417. msg = &twl->xfer_msg[1];
  418. msg->addr = twl->address;
  419. msg->flags = I2C_M_RD; /* Read the register value */
  420. msg->len = num_bytes; /* only n bytes */
  421. msg->buf = value;
  422. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  423. mutex_unlock(&twl->xfer_lock);
  424. /* i2cTransfer returns num messages.translate it pls.. */
  425. if (ret >= 0)
  426. ret = 0;
  427. return ret;
  428. }
  429. EXPORT_SYMBOL(twl4030_i2c_read);
  430. /**
  431. * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
  432. * @mod_no: module number
  433. * @value: the value to be written 8 bit
  434. * @reg: register address (just offset will do)
  435. *
  436. * Returns result of operation - 0 is success
  437. */
  438. int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  439. {
  440. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  441. u8 temp_buffer[2] = { 0 };
  442. /* offset 1 contains the data */
  443. temp_buffer[1] = value;
  444. return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
  445. }
  446. EXPORT_SYMBOL(twl4030_i2c_write_u8);
  447. /**
  448. * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
  449. * @mod_no: module number
  450. * @value: the value read 8 bit
  451. * @reg: register address (just offset will do)
  452. *
  453. * Returns result of operation - 0 is success
  454. */
  455. int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  456. {
  457. return twl4030_i2c_read(mod_no, value, reg, 1);
  458. }
  459. EXPORT_SYMBOL(twl4030_i2c_read_u8);
  460. /*----------------------------------------------------------------------*/
  461. static unsigned twl4030_irq_base;
  462. static struct completion irq_event;
  463. /*
  464. * This thread processes interrupts reported by the Primary Interrupt Handler.
  465. */
  466. static int twl4030_irq_thread(void *data)
  467. {
  468. long irq = (long)data;
  469. irq_desc_t *desc = irq_desc + irq;
  470. static unsigned i2c_errors;
  471. const static unsigned max_i2c_errors = 100;
  472. current->flags |= PF_NOFREEZE;
  473. while (!kthread_should_stop()) {
  474. int ret;
  475. int module_irq;
  476. u8 pih_isr;
  477. /* Wait for IRQ, then read PIH irq status (also blocking) */
  478. wait_for_completion_interruptible(&irq_event);
  479. ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
  480. REG_PIH_ISR_P1);
  481. if (ret) {
  482. pr_warning("%s: I2C error %d reading PIH ISR\n",
  483. DRIVER_NAME, ret);
  484. if (++i2c_errors >= max_i2c_errors) {
  485. printk(KERN_ERR "Maximum I2C error count"
  486. " exceeded. Terminating %s.\n",
  487. __func__);
  488. break;
  489. }
  490. complete(&irq_event);
  491. continue;
  492. }
  493. /* these handlers deal with the relevant SIH irq status */
  494. local_irq_disable();
  495. for (module_irq = twl4030_irq_base;
  496. pih_isr;
  497. pih_isr >>= 1, module_irq++) {
  498. if (pih_isr & 0x1) {
  499. irq_desc_t *d = irq_desc + module_irq;
  500. d->handle_irq(module_irq, d);
  501. }
  502. }
  503. local_irq_enable();
  504. desc->chip->unmask(irq);
  505. }
  506. return 0;
  507. }
  508. /*
  509. * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
  510. * This is a chained interrupt, so there is no desc->action method for it.
  511. * Now we need to query the interrupt controller in the twl4030 to determine
  512. * which module is generating the interrupt request. However, we can't do i2c
  513. * transactions in interrupt context, so we must defer that work to a kernel
  514. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  515. * the kernel thread.
  516. */
  517. static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
  518. {
  519. const unsigned int cpu = smp_processor_id();
  520. /*
  521. * Earlier this was desc->triggered = 1;
  522. */
  523. desc->status |= IRQ_LEVEL;
  524. /*
  525. * Acknowledge, clear _AND_ disable the interrupt.
  526. */
  527. desc->chip->ack(irq);
  528. if (!desc->depth) {
  529. kstat_cpu(cpu).irqs[irq]++;
  530. complete(&irq_event);
  531. }
  532. }
  533. static struct task_struct * __init start_twl4030_irq_thread(long irq)
  534. {
  535. struct task_struct *thread;
  536. init_completion(&irq_event);
  537. thread = kthread_run(twl4030_irq_thread, (void *)irq, "twl4030-irq");
  538. if (!thread)
  539. pr_err("%s: could not create twl4030 irq %ld thread!\n",
  540. DRIVER_NAME, irq);
  541. return thread;
  542. }
  543. /*----------------------------------------------------------------------*/
  544. static int add_children(struct twl4030_platform_data *pdata)
  545. {
  546. struct platform_device *pdev = NULL;
  547. struct twl4030_client *twl = NULL;
  548. int status = 0;
  549. if (twl_has_bci() && pdata->bci) {
  550. twl = &twl4030_modules[3];
  551. pdev = platform_device_alloc("twl4030_bci", -1);
  552. if (!pdev) {
  553. pr_debug("%s: can't alloc bci dev\n", DRIVER_NAME);
  554. status = -ENOMEM;
  555. goto err;
  556. }
  557. if (status == 0) {
  558. pdev->dev.parent = &twl->client->dev;
  559. status = platform_device_add_data(pdev, pdata->bci,
  560. sizeof(*pdata->bci));
  561. if (status < 0) {
  562. dev_dbg(&twl->client->dev,
  563. "can't add bci data, %d\n",
  564. status);
  565. goto err;
  566. }
  567. }
  568. if (status == 0) {
  569. struct resource r = {
  570. .start = TWL4030_PWRIRQ_CHG_PRES,
  571. .flags = IORESOURCE_IRQ,
  572. };
  573. status = platform_device_add_resources(pdev, &r, 1);
  574. }
  575. if (status == 0)
  576. status = platform_device_add(pdev);
  577. if (status < 0) {
  578. platform_device_put(pdev);
  579. dev_dbg(&twl->client->dev,
  580. "can't create bci dev, %d\n",
  581. status);
  582. goto err;
  583. }
  584. }
  585. if (twl_has_gpio() && pdata->gpio) {
  586. twl = &twl4030_modules[1];
  587. pdev = platform_device_alloc("twl4030_gpio", -1);
  588. if (!pdev) {
  589. pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
  590. status = -ENOMEM;
  591. goto err;
  592. }
  593. /* more driver model init */
  594. if (status == 0) {
  595. pdev->dev.parent = &twl->client->dev;
  596. /* device_init_wakeup(&pdev->dev, 1); */
  597. status = platform_device_add_data(pdev, pdata->gpio,
  598. sizeof(*pdata->gpio));
  599. if (status < 0) {
  600. dev_dbg(&twl->client->dev,
  601. "can't add gpio data, %d\n",
  602. status);
  603. goto err;
  604. }
  605. }
  606. /* GPIO module IRQ */
  607. if (status == 0) {
  608. struct resource r = {
  609. .start = pdata->irq_base + 0,
  610. .flags = IORESOURCE_IRQ,
  611. };
  612. status = platform_device_add_resources(pdev, &r, 1);
  613. }
  614. if (status == 0)
  615. status = platform_device_add(pdev);
  616. if (status < 0) {
  617. platform_device_put(pdev);
  618. dev_dbg(&twl->client->dev,
  619. "can't create gpio dev, %d\n",
  620. status);
  621. goto err;
  622. }
  623. }
  624. if (twl_has_keypad() && pdata->keypad) {
  625. pdev = platform_device_alloc("twl4030_keypad", -1);
  626. if (pdev) {
  627. twl = &twl4030_modules[2];
  628. pdev->dev.parent = &twl->client->dev;
  629. device_init_wakeup(&pdev->dev, 1);
  630. status = platform_device_add_data(pdev, pdata->keypad,
  631. sizeof(*pdata->keypad));
  632. if (status < 0) {
  633. dev_dbg(&twl->client->dev,
  634. "can't add keypad data, %d\n",
  635. status);
  636. platform_device_put(pdev);
  637. goto err;
  638. }
  639. status = platform_device_add(pdev);
  640. if (status < 0) {
  641. platform_device_put(pdev);
  642. dev_dbg(&twl->client->dev,
  643. "can't create keypad dev, %d\n",
  644. status);
  645. goto err;
  646. }
  647. } else {
  648. pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
  649. status = -ENOMEM;
  650. goto err;
  651. }
  652. }
  653. if (twl_has_madc() && pdata->madc) {
  654. pdev = platform_device_alloc("twl4030_madc", -1);
  655. if (pdev) {
  656. twl = &twl4030_modules[2];
  657. pdev->dev.parent = &twl->client->dev;
  658. device_init_wakeup(&pdev->dev, 1);
  659. status = platform_device_add_data(pdev, pdata->madc,
  660. sizeof(*pdata->madc));
  661. if (status < 0) {
  662. platform_device_put(pdev);
  663. dev_dbg(&twl->client->dev,
  664. "can't add madc data, %d\n",
  665. status);
  666. goto err;
  667. }
  668. status = platform_device_add(pdev);
  669. if (status < 0) {
  670. platform_device_put(pdev);
  671. dev_dbg(&twl->client->dev,
  672. "can't create madc dev, %d\n",
  673. status);
  674. goto err;
  675. }
  676. } else {
  677. pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
  678. status = -ENOMEM;
  679. goto err;
  680. }
  681. }
  682. if (twl_has_rtc()) {
  683. twl = &twl4030_modules[3];
  684. pdev = platform_device_alloc("twl4030_rtc", -1);
  685. if (!pdev) {
  686. pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
  687. status = -ENOMEM;
  688. } else {
  689. pdev->dev.parent = &twl->client->dev;
  690. device_init_wakeup(&pdev->dev, 1);
  691. }
  692. /*
  693. * REVISIT platform_data here currently might use of
  694. * "msecure" line ... but for now we just expect board
  695. * setup to tell the chip "we are secure" at all times.
  696. * Eventually, Linux might become more aware of such
  697. * HW security concerns, and "least privilege".
  698. */
  699. /* RTC module IRQ */
  700. if (status == 0) {
  701. struct resource r = {
  702. /* REVISIT don't hard-wire this stuff */
  703. .start = TWL4030_PWRIRQ_RTC,
  704. .flags = IORESOURCE_IRQ,
  705. };
  706. status = platform_device_add_resources(pdev, &r, 1);
  707. }
  708. if (status == 0)
  709. status = platform_device_add(pdev);
  710. if (status < 0) {
  711. platform_device_put(pdev);
  712. dev_dbg(&twl->client->dev,
  713. "can't create rtc dev, %d\n",
  714. status);
  715. goto err;
  716. }
  717. }
  718. if (twl_has_usb() && pdata->usb) {
  719. twl = &twl4030_modules[0];
  720. pdev = platform_device_alloc("twl4030_usb", -1);
  721. if (!pdev) {
  722. pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
  723. status = -ENOMEM;
  724. goto err;
  725. }
  726. if (status == 0) {
  727. pdev->dev.parent = &twl->client->dev;
  728. device_init_wakeup(&pdev->dev, 1);
  729. status = platform_device_add_data(pdev, pdata->usb,
  730. sizeof(*pdata->usb));
  731. if (status < 0) {
  732. platform_device_put(pdev);
  733. dev_dbg(&twl->client->dev,
  734. "can't add usb data, %d\n",
  735. status);
  736. goto err;
  737. }
  738. }
  739. if (status == 0) {
  740. struct resource r = {
  741. .start = TWL4030_PWRIRQ_USB_PRES,
  742. .flags = IORESOURCE_IRQ,
  743. };
  744. status = platform_device_add_resources(pdev, &r, 1);
  745. }
  746. if (status == 0)
  747. status = platform_device_add(pdev);
  748. if (status < 0) {
  749. platform_device_put(pdev);
  750. dev_dbg(&twl->client->dev,
  751. "can't create usb dev, %d\n",
  752. status);
  753. }
  754. }
  755. err:
  756. if (status)
  757. pr_err("failed to add twl4030's children (status %d)\n", status);
  758. return status;
  759. }
  760. /*----------------------------------------------------------------------*/
  761. /*
  762. * These three functions initialize the on-chip clock framework,
  763. * letting it generate the right frequencies for USB, MADC, and
  764. * other purposes.
  765. */
  766. static inline int __init protect_pm_master(void)
  767. {
  768. int e = 0;
  769. e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
  770. R_PROTECT_KEY);
  771. return e;
  772. }
  773. static inline int __init unprotect_pm_master(void)
  774. {
  775. int e = 0;
  776. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
  777. R_PROTECT_KEY);
  778. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
  779. R_PROTECT_KEY);
  780. return e;
  781. }
  782. static void __init clocks_init(void)
  783. {
  784. int e = 0;
  785. struct clk *osc;
  786. u32 rate;
  787. u8 ctrl = HFCLK_FREQ_26_MHZ;
  788. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  789. if (cpu_is_omap2430())
  790. osc = clk_get(NULL, "osc_ck");
  791. else
  792. osc = clk_get(NULL, "osc_sys_ck");
  793. #else
  794. /* REVISIT for non-OMAP systems, pass the clock rate from
  795. * board init code, using platform_data.
  796. */
  797. osc = ERR_PTR(-EIO);
  798. #endif
  799. if (IS_ERR(osc)) {
  800. printk(KERN_WARNING "Skipping twl4030 internal clock init and "
  801. "using bootloader value (unknown osc rate)\n");
  802. return;
  803. }
  804. rate = clk_get_rate(osc);
  805. clk_put(osc);
  806. switch (rate) {
  807. case 19200000:
  808. ctrl = HFCLK_FREQ_19p2_MHZ;
  809. break;
  810. case 26000000:
  811. ctrl = HFCLK_FREQ_26_MHZ;
  812. break;
  813. case 38400000:
  814. ctrl = HFCLK_FREQ_38p4_MHZ;
  815. break;
  816. }
  817. ctrl |= HIGH_PERF_SQ;
  818. e |= unprotect_pm_master();
  819. /* effect->MADC+USB ck en */
  820. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  821. e |= protect_pm_master();
  822. if (e < 0)
  823. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  824. }
  825. /*----------------------------------------------------------------------*/
  826. /**
  827. * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
  828. * @mod_no: TWL4030 module number
  829. * @reg: register index to clear
  830. * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
  831. *
  832. * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
  833. * status register to ensure that any prior interrupts are cleared.
  834. * Returns the status from the I2C read operation.
  835. */
  836. static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
  837. {
  838. u8 tmp;
  839. return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
  840. twl4030_i2c_write_u8(mod_no, 0xff, reg);
  841. }
  842. /**
  843. * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
  844. * @mod_no: TWL4030 module number
  845. * @reg: register index to clear
  846. *
  847. * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
  848. * the specified TWL module are cleared by reads, or 0 if cleared by
  849. * writes.
  850. */
  851. static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
  852. {
  853. u8 tmp = 0;
  854. WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
  855. tmp &= TWL4030_SIH_CTRL_COR_MASK;
  856. tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
  857. return tmp;
  858. }
  859. /**
  860. * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
  861. * @t: pointer to twl4030_mod_iregs array
  862. * @t_sz: ARRAY_SIZE(t) (starting at 1)
  863. *
  864. * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
  865. * interrupt status registers (ISRs). No return value, but will WARN if
  866. * any I2C operations fail.
  867. */
  868. static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
  869. const u8 t_sz)
  870. {
  871. int i, j;
  872. /*
  873. * N.B. - further efficiency is possible here. Eight I2C
  874. * operations on BCI and GPIO modules are avoidable if I2C
  875. * burst read/write transactions were implemented. Would
  876. * probably save about 1ms of boot time and a small amount of
  877. * power.
  878. */
  879. for (i = 0; i < t_sz; i++) {
  880. const struct twl4030_mod_iregs tmr = t[i];
  881. int cor;
  882. /* Are ISRs cleared by reads or writes? */
  883. cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
  884. for (j = 0; j < tmr.reg_cnt; j++) {
  885. /* Mask interrupts at the TWL4030 */
  886. WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
  887. tmr.imrs[j]) < 0);
  888. /* Clear TWL4030 ISRs */
  889. WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
  890. tmr.isrs[j], cor) < 0);
  891. }
  892. }
  893. }
  894. static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
  895. {
  896. int i;
  897. /*
  898. * Mask and clear all TWL4030 interrupts since initially we do
  899. * not have any TWL4030 module interrupt handlers present
  900. */
  901. twl4030_mask_clear_intrs(twl4030_mod_regs,
  902. ARRAY_SIZE(twl4030_mod_regs));
  903. twl4030_irq_base = irq_base;
  904. /* install an irq handler for each of the PIH modules */
  905. for (i = irq_base; i < irq_end; i++) {
  906. set_irq_chip_and_handler(i, &twl4030_irq_chip,
  907. handle_simple_irq);
  908. activate_irq(i);
  909. }
  910. /* install an irq handler to demultiplex the TWL4030 interrupt */
  911. set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
  912. set_irq_chained_handler(irq_num, do_twl4030_irq);
  913. }
  914. /*----------------------------------------------------------------------*/
  915. static int twl4030_remove(struct i2c_client *client)
  916. {
  917. unsigned i;
  918. /* FIXME undo twl_init_irq() */
  919. if (twl4030_irq_base) {
  920. dev_err(&client->dev, "can't yet clean up IRQs?\n");
  921. return -ENOSYS;
  922. }
  923. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  924. struct twl4030_client *twl = &twl4030_modules[i];
  925. if (twl->client && twl->client != client)
  926. i2c_unregister_device(twl->client);
  927. twl4030_modules[i].client = NULL;
  928. }
  929. inuse = false;
  930. return 0;
  931. }
  932. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  933. static int
  934. twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
  935. {
  936. int status;
  937. unsigned i;
  938. struct twl4030_platform_data *pdata = client->dev.platform_data;
  939. if (!pdata) {
  940. dev_dbg(&client->dev, "no platform data?\n");
  941. return -EINVAL;
  942. }
  943. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  944. dev_dbg(&client->dev, "can't talk I2C?\n");
  945. return -EIO;
  946. }
  947. if (inuse || twl4030_irq_base) {
  948. dev_dbg(&client->dev, "driver is already in use\n");
  949. return -EBUSY;
  950. }
  951. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  952. struct twl4030_client *twl = &twl4030_modules[i];
  953. twl->address = client->addr + i;
  954. if (i == 0)
  955. twl->client = client;
  956. else {
  957. twl->client = i2c_new_dummy(client->adapter,
  958. twl->address);
  959. if (!twl->client) {
  960. dev_err(&twl->client->dev,
  961. "can't attach client %d\n", i);
  962. status = -ENOMEM;
  963. goto fail;
  964. }
  965. strlcpy(twl->client->name, id->name,
  966. sizeof(twl->client->name));
  967. }
  968. mutex_init(&twl->xfer_lock);
  969. }
  970. inuse = true;
  971. /* setup clock framework */
  972. clocks_init();
  973. /* Maybe init the T2 Interrupt subsystem */
  974. if (client->irq
  975. && pdata->irq_base
  976. && pdata->irq_end > pdata->irq_base) {
  977. twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
  978. dev_info(&client->dev, "IRQ %d chains IRQs %d..%d\n",
  979. client->irq, pdata->irq_base, pdata->irq_end - 1);
  980. }
  981. status = add_children(pdata);
  982. fail:
  983. if (status < 0)
  984. twl4030_remove(client);
  985. return status;
  986. }
  987. static const struct i2c_device_id twl4030_ids[] = {
  988. { "twl4030", 0 }, /* "Triton 2" */
  989. { "tps65950", 0 }, /* catalog version of twl4030 */
  990. { "tps65930", 0 }, /* fewer LDOs and DACs; no charger */
  991. { "tps65920", 0 }, /* fewer LDOs; no codec or charger */
  992. { "twl5030", 0 }, /* T2 updated */
  993. { /* end of list */ },
  994. };
  995. MODULE_DEVICE_TABLE(i2c, twl4030_ids);
  996. /* One Client Driver , 4 Clients */
  997. static struct i2c_driver twl4030_driver = {
  998. .driver.name = DRIVER_NAME,
  999. .id_table = twl4030_ids,
  1000. .probe = twl4030_probe,
  1001. .remove = twl4030_remove,
  1002. };
  1003. static int __init twl4030_init(void)
  1004. {
  1005. return i2c_add_driver(&twl4030_driver);
  1006. }
  1007. subsys_initcall(twl4030_init);
  1008. static void __exit twl4030_exit(void)
  1009. {
  1010. i2c_del_driver(&twl4030_driver);
  1011. }
  1012. module_exit(twl4030_exit);
  1013. MODULE_AUTHOR("Texas Instruments, Inc.");
  1014. MODULE_DESCRIPTION("I2C Core interface for TWL4030");
  1015. MODULE_LICENSE("GPL");