twl-core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
  3. * and audio CODEC devices
  4. *
  5. * Copyright (C) 2005-2006 Texas Instruments, Inc.
  6. *
  7. * Modifications to defer interrupt handling to a kernel thread:
  8. * Copyright (C) 2006 MontaVista Software, Inc.
  9. *
  10. * Based on tlv320aic23.c:
  11. * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
  12. *
  13. * Code cleanup and modifications to IRQ handler.
  14. * by syed khasim <x0khasim@ti.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <linux/init.h>
  31. #include <linux/mutex.h>
  32. #include <linux/module.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/clk.h>
  35. #include <linux/err.h>
  36. #include <linux/device.h>
  37. #include <linux/of.h>
  38. #include <linux/of_irq.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/irqdomain.h>
  41. #include <linux/regulator/machine.h>
  42. #include <linux/i2c.h>
  43. #include <linux/i2c/twl.h>
  44. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  45. #include <plat/cpu.h>
  46. #endif
  47. /*
  48. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  49. * Management and System Companion Device" chips originally designed for
  50. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  51. * often at around 3 Mbit/sec, including for interrupt handling.
  52. *
  53. * This driver core provides genirq support for the interrupts emitted,
  54. * by the various modules, and exports register access primitives.
  55. *
  56. * FIXME this driver currently requires use of the first interrupt line
  57. * (and associated registers).
  58. */
  59. #define DRIVER_NAME "twl"
  60. #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
  61. #define twl_has_keypad() true
  62. #else
  63. #define twl_has_keypad() false
  64. #endif
  65. #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
  66. #define twl_has_gpio() true
  67. #else
  68. #define twl_has_gpio() false
  69. #endif
  70. #if defined(CONFIG_REGULATOR_TWL4030) \
  71. || defined(CONFIG_REGULATOR_TWL4030_MODULE)
  72. #define twl_has_regulator() true
  73. #else
  74. #define twl_has_regulator() false
  75. #endif
  76. #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
  77. #define twl_has_madc() true
  78. #else
  79. #define twl_has_madc() false
  80. #endif
  81. #ifdef CONFIG_TWL4030_POWER
  82. #define twl_has_power() true
  83. #else
  84. #define twl_has_power() false
  85. #endif
  86. #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
  87. #define twl_has_rtc() true
  88. #else
  89. #define twl_has_rtc() false
  90. #endif
  91. #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE) ||\
  92. defined(CONFIG_TWL6030_USB) || defined(CONFIG_TWL6030_USB_MODULE)
  93. #define twl_has_usb() true
  94. #else
  95. #define twl_has_usb() false
  96. #endif
  97. #if defined(CONFIG_TWL4030_WATCHDOG) || \
  98. defined(CONFIG_TWL4030_WATCHDOG_MODULE)
  99. #define twl_has_watchdog() true
  100. #else
  101. #define twl_has_watchdog() false
  102. #endif
  103. #if defined(CONFIG_MFD_TWL4030_AUDIO) || defined(CONFIG_MFD_TWL4030_AUDIO_MODULE) ||\
  104. defined(CONFIG_TWL6040_CORE) || defined(CONFIG_TWL6040_CORE_MODULE)
  105. #define twl_has_codec() true
  106. #else
  107. #define twl_has_codec() false
  108. #endif
  109. #if defined(CONFIG_CHARGER_TWL4030) || defined(CONFIG_CHARGER_TWL4030_MODULE)
  110. #define twl_has_bci() true
  111. #else
  112. #define twl_has_bci() false
  113. #endif
  114. /* Triton Core internal information (BEGIN) */
  115. /* Last - for index max*/
  116. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  117. #define TWL_NUM_SLAVES 4
  118. #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
  119. || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
  120. #define twl_has_pwrbutton() true
  121. #else
  122. #define twl_has_pwrbutton() false
  123. #endif
  124. #define SUB_CHIP_ID0 0
  125. #define SUB_CHIP_ID1 1
  126. #define SUB_CHIP_ID2 2
  127. #define SUB_CHIP_ID3 3
  128. #define TWL_MODULE_LAST TWL4030_MODULE_LAST
  129. #define TWL4030_NR_IRQS 8
  130. #define TWL6030_NR_IRQS 20
  131. /* Base Address defns for twl4030_map[] */
  132. /* subchip/slave 0 - USB ID */
  133. #define TWL4030_BASEADD_USB 0x0000
  134. /* subchip/slave 1 - AUD ID */
  135. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  136. #define TWL4030_BASEADD_GPIO 0x0098
  137. #define TWL4030_BASEADD_INTBR 0x0085
  138. #define TWL4030_BASEADD_PIH 0x0080
  139. #define TWL4030_BASEADD_TEST 0x004C
  140. /* subchip/slave 2 - AUX ID */
  141. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  142. #define TWL4030_BASEADD_LED 0x00EE
  143. #define TWL4030_BASEADD_MADC 0x0000
  144. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  145. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  146. #define TWL4030_BASEADD_PWM0 0x00F8
  147. #define TWL4030_BASEADD_PWM1 0x00FB
  148. #define TWL4030_BASEADD_PWMA 0x00EF
  149. #define TWL4030_BASEADD_PWMB 0x00F1
  150. #define TWL4030_BASEADD_KEYPAD 0x00D2
  151. #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
  152. #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
  153. one */
  154. /* subchip/slave 3 - POWER ID */
  155. #define TWL4030_BASEADD_BACKUP 0x0014
  156. #define TWL4030_BASEADD_INT 0x002E
  157. #define TWL4030_BASEADD_PM_MASTER 0x0036
  158. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  159. #define TWL4030_BASEADD_RTC 0x001C
  160. #define TWL4030_BASEADD_SECURED_REG 0x0000
  161. /* Triton Core internal information (END) */
  162. /* subchip/slave 0 0x48 - POWER */
  163. #define TWL6030_BASEADD_RTC 0x0000
  164. #define TWL6030_BASEADD_MEM 0x0017
  165. #define TWL6030_BASEADD_PM_MASTER 0x001F
  166. #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
  167. #define TWL6030_BASEADD_PM_MISC 0x00E2
  168. #define TWL6030_BASEADD_PM_PUPD 0x00F0
  169. /* subchip/slave 1 0x49 - FEATURE */
  170. #define TWL6030_BASEADD_USB 0x0000
  171. #define TWL6030_BASEADD_GPADC_CTRL 0x002E
  172. #define TWL6030_BASEADD_AUX 0x0090
  173. #define TWL6030_BASEADD_PWM 0x00BA
  174. #define TWL6030_BASEADD_GASGAUGE 0x00C0
  175. #define TWL6030_BASEADD_PIH 0x00D0
  176. #define TWL6030_BASEADD_CHARGER 0x00E0
  177. #define TWL6025_BASEADD_CHARGER 0x00DA
  178. /* subchip/slave 2 0x4A - DFT */
  179. #define TWL6030_BASEADD_DIEID 0x00C0
  180. /* subchip/slave 3 0x4B - AUDIO */
  181. #define TWL6030_BASEADD_AUDIO 0x0000
  182. #define TWL6030_BASEADD_RSV 0x0000
  183. #define TWL6030_BASEADD_ZERO 0x0000
  184. /* Few power values */
  185. #define R_CFG_BOOT 0x05
  186. /* some fields in R_CFG_BOOT */
  187. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  188. #define HFCLK_FREQ_26_MHZ (2 << 0)
  189. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  190. #define HIGH_PERF_SQ (1 << 3)
  191. #define CK32K_LOWPWR_EN (1 << 7)
  192. /* chip-specific feature flags, for i2c_device_id.driver_data */
  193. #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
  194. #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
  195. #define TWL5031 BIT(2) /* twl5031 has different registers */
  196. #define TWL6030_CLASS BIT(3) /* TWL6030 class */
  197. /*----------------------------------------------------------------------*/
  198. /* is driver active, bound to a chip? */
  199. static bool inuse;
  200. /* TWL IDCODE Register value */
  201. static u32 twl_idcode;
  202. static unsigned int twl_id;
  203. unsigned int twl_rev(void)
  204. {
  205. return twl_id;
  206. }
  207. EXPORT_SYMBOL(twl_rev);
  208. /* Structure for each TWL4030/TWL6030 Slave */
  209. struct twl_client {
  210. struct i2c_client *client;
  211. u8 address;
  212. /* max numb of i2c_msg required is for read =2 */
  213. struct i2c_msg xfer_msg[2];
  214. /* To lock access to xfer_msg */
  215. struct mutex xfer_lock;
  216. };
  217. static struct twl_client twl_modules[TWL_NUM_SLAVES];
  218. #ifdef CONFIG_IRQ_DOMAIN
  219. static struct irq_domain domain;
  220. #endif
  221. /* mapping the module id to slave id and base address */
  222. struct twl_mapping {
  223. unsigned char sid; /* Slave ID */
  224. unsigned char base; /* base address */
  225. };
  226. static struct twl_mapping *twl_map;
  227. static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  228. /*
  229. * NOTE: don't change this table without updating the
  230. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  231. * so they continue to match the order in this table.
  232. */
  233. { 0, TWL4030_BASEADD_USB },
  234. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  235. { 1, TWL4030_BASEADD_GPIO },
  236. { 1, TWL4030_BASEADD_INTBR },
  237. { 1, TWL4030_BASEADD_PIH },
  238. { 1, TWL4030_BASEADD_TEST },
  239. { 2, TWL4030_BASEADD_KEYPAD },
  240. { 2, TWL4030_BASEADD_MADC },
  241. { 2, TWL4030_BASEADD_INTERRUPTS },
  242. { 2, TWL4030_BASEADD_LED },
  243. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  244. { 2, TWL4030_BASEADD_PRECHARGE },
  245. { 2, TWL4030_BASEADD_PWM0 },
  246. { 2, TWL4030_BASEADD_PWM1 },
  247. { 2, TWL4030_BASEADD_PWMA },
  248. { 2, TWL4030_BASEADD_PWMB },
  249. { 2, TWL5031_BASEADD_ACCESSORY },
  250. { 2, TWL5031_BASEADD_INTERRUPTS },
  251. { 3, TWL4030_BASEADD_BACKUP },
  252. { 3, TWL4030_BASEADD_INT },
  253. { 3, TWL4030_BASEADD_PM_MASTER },
  254. { 3, TWL4030_BASEADD_PM_RECEIVER },
  255. { 3, TWL4030_BASEADD_RTC },
  256. { 3, TWL4030_BASEADD_SECURED_REG },
  257. };
  258. static struct twl_mapping twl6030_map[] = {
  259. /*
  260. * NOTE: don't change this table without updating the
  261. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  262. * so they continue to match the order in this table.
  263. */
  264. { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
  265. { SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
  266. { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
  267. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  268. { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
  269. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  270. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  271. { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
  272. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  273. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  274. { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
  275. { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
  276. { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
  277. { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
  278. { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
  279. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  280. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  281. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  282. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  283. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  284. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
  285. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
  286. { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
  287. { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
  288. { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
  289. };
  290. /*----------------------------------------------------------------------*/
  291. /* Exported Functions */
  292. /**
  293. * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
  294. * @mod_no: module number
  295. * @value: an array of num_bytes+1 containing data to write
  296. * @reg: register address (just offset will do)
  297. * @num_bytes: number of bytes to transfer
  298. *
  299. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  300. * valid data starts at Offset 1.
  301. *
  302. * Returns the result of operation - 0 is success
  303. */
  304. int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  305. {
  306. int ret;
  307. int sid;
  308. struct twl_client *twl;
  309. struct i2c_msg *msg;
  310. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  311. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  312. return -EPERM;
  313. }
  314. if (unlikely(!inuse)) {
  315. pr_err("%s: not initialized\n", DRIVER_NAME);
  316. return -EPERM;
  317. }
  318. sid = twl_map[mod_no].sid;
  319. twl = &twl_modules[sid];
  320. mutex_lock(&twl->xfer_lock);
  321. /*
  322. * [MSG1]: fill the register address data
  323. * fill the data Tx buffer
  324. */
  325. msg = &twl->xfer_msg[0];
  326. msg->addr = twl->address;
  327. msg->len = num_bytes + 1;
  328. msg->flags = 0;
  329. msg->buf = value;
  330. /* over write the first byte of buffer with the register address */
  331. *value = twl_map[mod_no].base + reg;
  332. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  333. mutex_unlock(&twl->xfer_lock);
  334. /* i2c_transfer returns number of messages transferred */
  335. if (ret != 1) {
  336. pr_err("%s: i2c_write failed to transfer all messages\n",
  337. DRIVER_NAME);
  338. if (ret < 0)
  339. return ret;
  340. else
  341. return -EIO;
  342. } else {
  343. return 0;
  344. }
  345. }
  346. EXPORT_SYMBOL(twl_i2c_write);
  347. /**
  348. * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
  349. * @mod_no: module number
  350. * @value: an array of num_bytes containing data to be read
  351. * @reg: register address (just offset will do)
  352. * @num_bytes: number of bytes to transfer
  353. *
  354. * Returns result of operation - num_bytes is success else failure.
  355. */
  356. int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  357. {
  358. int ret;
  359. u8 val;
  360. int sid;
  361. struct twl_client *twl;
  362. struct i2c_msg *msg;
  363. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  364. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  365. return -EPERM;
  366. }
  367. if (unlikely(!inuse)) {
  368. pr_err("%s: not initialized\n", DRIVER_NAME);
  369. return -EPERM;
  370. }
  371. sid = twl_map[mod_no].sid;
  372. twl = &twl_modules[sid];
  373. mutex_lock(&twl->xfer_lock);
  374. /* [MSG1] fill the register address data */
  375. msg = &twl->xfer_msg[0];
  376. msg->addr = twl->address;
  377. msg->len = 1;
  378. msg->flags = 0; /* Read the register value */
  379. val = twl_map[mod_no].base + reg;
  380. msg->buf = &val;
  381. /* [MSG2] fill the data rx buffer */
  382. msg = &twl->xfer_msg[1];
  383. msg->addr = twl->address;
  384. msg->flags = I2C_M_RD; /* Read the register value */
  385. msg->len = num_bytes; /* only n bytes */
  386. msg->buf = value;
  387. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  388. mutex_unlock(&twl->xfer_lock);
  389. /* i2c_transfer returns number of messages transferred */
  390. if (ret != 2) {
  391. pr_err("%s: i2c_read failed to transfer all messages\n",
  392. DRIVER_NAME);
  393. if (ret < 0)
  394. return ret;
  395. else
  396. return -EIO;
  397. } else {
  398. return 0;
  399. }
  400. }
  401. EXPORT_SYMBOL(twl_i2c_read);
  402. /**
  403. * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
  404. * @mod_no: module number
  405. * @value: the value to be written 8 bit
  406. * @reg: register address (just offset will do)
  407. *
  408. * Returns result of operation - 0 is success
  409. */
  410. int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  411. {
  412. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  413. u8 temp_buffer[2] = { 0 };
  414. /* offset 1 contains the data */
  415. temp_buffer[1] = value;
  416. return twl_i2c_write(mod_no, temp_buffer, reg, 1);
  417. }
  418. EXPORT_SYMBOL(twl_i2c_write_u8);
  419. /**
  420. * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
  421. * @mod_no: module number
  422. * @value: the value read 8 bit
  423. * @reg: register address (just offset will do)
  424. *
  425. * Returns result of operation - 0 is success
  426. */
  427. int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  428. {
  429. return twl_i2c_read(mod_no, value, reg, 1);
  430. }
  431. EXPORT_SYMBOL(twl_i2c_read_u8);
  432. /*----------------------------------------------------------------------*/
  433. /**
  434. * twl_read_idcode_register - API to read the IDCODE register.
  435. *
  436. * Unlocks the IDCODE register and read the 32 bit value.
  437. */
  438. static int twl_read_idcode_register(void)
  439. {
  440. int err;
  441. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
  442. REG_UNLOCK_TEST_REG);
  443. if (err) {
  444. pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
  445. goto fail;
  446. }
  447. err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
  448. REG_IDCODE_7_0, 4);
  449. if (err) {
  450. pr_err("TWL4030: unable to read IDCODE -%d\n", err);
  451. goto fail;
  452. }
  453. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
  454. if (err)
  455. pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
  456. fail:
  457. return err;
  458. }
  459. /**
  460. * twl_get_type - API to get TWL Si type.
  461. *
  462. * Api to get the TWL Si type from IDCODE value.
  463. */
  464. int twl_get_type(void)
  465. {
  466. return TWL_SIL_TYPE(twl_idcode);
  467. }
  468. EXPORT_SYMBOL_GPL(twl_get_type);
  469. /**
  470. * twl_get_version - API to get TWL Si version.
  471. *
  472. * Api to get the TWL Si version from IDCODE value.
  473. */
  474. int twl_get_version(void)
  475. {
  476. return TWL_SIL_REV(twl_idcode);
  477. }
  478. EXPORT_SYMBOL_GPL(twl_get_version);
  479. static struct device *
  480. add_numbered_child(unsigned chip, const char *name, int num,
  481. void *pdata, unsigned pdata_len,
  482. bool can_wakeup, int irq0, int irq1)
  483. {
  484. struct platform_device *pdev;
  485. struct twl_client *twl = &twl_modules[chip];
  486. int status;
  487. pdev = platform_device_alloc(name, num);
  488. if (!pdev) {
  489. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  490. status = -ENOMEM;
  491. goto err;
  492. }
  493. device_init_wakeup(&pdev->dev, can_wakeup);
  494. pdev->dev.parent = &twl->client->dev;
  495. if (pdata) {
  496. status = platform_device_add_data(pdev, pdata, pdata_len);
  497. if (status < 0) {
  498. dev_dbg(&pdev->dev, "can't add platform_data\n");
  499. goto err;
  500. }
  501. }
  502. if (irq0) {
  503. struct resource r[2] = {
  504. { .start = irq0, .flags = IORESOURCE_IRQ, },
  505. { .start = irq1, .flags = IORESOURCE_IRQ, },
  506. };
  507. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  508. if (status < 0) {
  509. dev_dbg(&pdev->dev, "can't add irqs\n");
  510. goto err;
  511. }
  512. }
  513. status = platform_device_add(pdev);
  514. err:
  515. if (status < 0) {
  516. platform_device_put(pdev);
  517. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  518. return ERR_PTR(status);
  519. }
  520. return &pdev->dev;
  521. }
  522. static inline struct device *add_child(unsigned chip, const char *name,
  523. void *pdata, unsigned pdata_len,
  524. bool can_wakeup, int irq0, int irq1)
  525. {
  526. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  527. can_wakeup, irq0, irq1);
  528. }
  529. static struct device *
  530. add_regulator_linked(int num, struct regulator_init_data *pdata,
  531. struct regulator_consumer_supply *consumers,
  532. unsigned num_consumers, unsigned long features)
  533. {
  534. unsigned sub_chip_id;
  535. /* regulator framework demands init_data ... */
  536. if (!pdata)
  537. return NULL;
  538. if (consumers) {
  539. pdata->consumer_supplies = consumers;
  540. pdata->num_consumer_supplies = num_consumers;
  541. }
  542. pdata->driver_data = (void *)features;
  543. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  544. sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
  545. return add_numbered_child(sub_chip_id, "twl_reg", num,
  546. pdata, sizeof(*pdata), false, 0, 0);
  547. }
  548. static struct device *
  549. add_regulator(int num, struct regulator_init_data *pdata,
  550. unsigned long features)
  551. {
  552. return add_regulator_linked(num, pdata, NULL, 0, features);
  553. }
  554. /*
  555. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  556. * for the PIH, and the next are for the PWR_INT SIH, since
  557. * that's how twl_init_irq() sets things up.
  558. */
  559. static int
  560. add_children(struct twl4030_platform_data *pdata, unsigned long features)
  561. {
  562. struct device *child;
  563. unsigned sub_chip_id;
  564. if (twl_has_gpio() && pdata->gpio) {
  565. child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
  566. pdata->gpio, sizeof(*pdata->gpio),
  567. false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
  568. if (IS_ERR(child))
  569. return PTR_ERR(child);
  570. }
  571. if (twl_has_keypad() && pdata->keypad) {
  572. child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
  573. pdata->keypad, sizeof(*pdata->keypad),
  574. true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
  575. if (IS_ERR(child))
  576. return PTR_ERR(child);
  577. }
  578. if (twl_has_madc() && pdata->madc) {
  579. child = add_child(2, "twl4030_madc",
  580. pdata->madc, sizeof(*pdata->madc),
  581. true, pdata->irq_base + MADC_INTR_OFFSET, 0);
  582. if (IS_ERR(child))
  583. return PTR_ERR(child);
  584. }
  585. if (twl_has_rtc()) {
  586. /*
  587. * REVISIT platform_data here currently might expose the
  588. * "msecure" line ... but for now we just expect board
  589. * setup to tell the chip "it's always ok to SET_TIME".
  590. * Eventually, Linux might become more aware of such
  591. * HW security concerns, and "least privilege".
  592. */
  593. sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
  594. child = add_child(sub_chip_id, "twl_rtc",
  595. NULL, 0,
  596. true, pdata->irq_base + RTC_INTR_OFFSET, 0);
  597. if (IS_ERR(child))
  598. return PTR_ERR(child);
  599. }
  600. if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
  601. static struct regulator_consumer_supply usb1v5 = {
  602. .supply = "usb1v5",
  603. };
  604. static struct regulator_consumer_supply usb1v8 = {
  605. .supply = "usb1v8",
  606. };
  607. static struct regulator_consumer_supply usb3v1 = {
  608. .supply = "usb3v1",
  609. };
  610. /* First add the regulators so that they can be used by transceiver */
  611. if (twl_has_regulator()) {
  612. /* this is a template that gets copied */
  613. struct regulator_init_data usb_fixed = {
  614. .constraints.valid_modes_mask =
  615. REGULATOR_MODE_NORMAL
  616. | REGULATOR_MODE_STANDBY,
  617. .constraints.valid_ops_mask =
  618. REGULATOR_CHANGE_MODE
  619. | REGULATOR_CHANGE_STATUS,
  620. };
  621. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  622. &usb_fixed, &usb1v5, 1,
  623. features);
  624. if (IS_ERR(child))
  625. return PTR_ERR(child);
  626. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  627. &usb_fixed, &usb1v8, 1,
  628. features);
  629. if (IS_ERR(child))
  630. return PTR_ERR(child);
  631. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  632. &usb_fixed, &usb3v1, 1,
  633. features);
  634. if (IS_ERR(child))
  635. return PTR_ERR(child);
  636. }
  637. child = add_child(0, "twl4030_usb",
  638. pdata->usb, sizeof(*pdata->usb),
  639. true,
  640. /* irq0 = USB_PRES, irq1 = USB */
  641. pdata->irq_base + USB_PRES_INTR_OFFSET,
  642. pdata->irq_base + USB_INTR_OFFSET);
  643. if (IS_ERR(child))
  644. return PTR_ERR(child);
  645. /* we need to connect regulators to this transceiver */
  646. if (twl_has_regulator() && child) {
  647. usb1v5.dev = child;
  648. usb1v8.dev = child;
  649. usb3v1.dev = child;
  650. }
  651. }
  652. if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
  653. static struct regulator_consumer_supply usb3v3;
  654. int regulator;
  655. if (twl_has_regulator()) {
  656. /* this is a template that gets copied */
  657. struct regulator_init_data usb_fixed = {
  658. .constraints.valid_modes_mask =
  659. REGULATOR_MODE_NORMAL
  660. | REGULATOR_MODE_STANDBY,
  661. .constraints.valid_ops_mask =
  662. REGULATOR_CHANGE_MODE
  663. | REGULATOR_CHANGE_STATUS,
  664. };
  665. if (features & TWL6025_SUBCLASS) {
  666. usb3v3.supply = "ldousb";
  667. regulator = TWL6025_REG_LDOUSB;
  668. } else {
  669. usb3v3.supply = "vusb";
  670. regulator = TWL6030_REG_VUSB;
  671. }
  672. child = add_regulator_linked(regulator, &usb_fixed,
  673. &usb3v3, 1,
  674. features);
  675. if (IS_ERR(child))
  676. return PTR_ERR(child);
  677. }
  678. pdata->usb->features = features;
  679. child = add_child(0, "twl6030_usb",
  680. pdata->usb, sizeof(*pdata->usb),
  681. true,
  682. /* irq1 = VBUS_PRES, irq0 = USB ID */
  683. pdata->irq_base + USBOTG_INTR_OFFSET,
  684. pdata->irq_base + USB_PRES_INTR_OFFSET);
  685. if (IS_ERR(child))
  686. return PTR_ERR(child);
  687. /* we need to connect regulators to this transceiver */
  688. if (twl_has_regulator() && child)
  689. usb3v3.dev = child;
  690. } else if (twl_has_regulator() && twl_class_is_6030()) {
  691. if (features & TWL6025_SUBCLASS)
  692. child = add_regulator(TWL6025_REG_LDOUSB,
  693. pdata->ldousb, features);
  694. else
  695. child = add_regulator(TWL6030_REG_VUSB,
  696. pdata->vusb, features);
  697. if (IS_ERR(child))
  698. return PTR_ERR(child);
  699. }
  700. if (twl_has_watchdog() && twl_class_is_4030()) {
  701. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  702. if (IS_ERR(child))
  703. return PTR_ERR(child);
  704. }
  705. if (twl_has_pwrbutton() && twl_class_is_4030()) {
  706. child = add_child(1, "twl4030_pwrbutton",
  707. NULL, 0, true, pdata->irq_base + 8 + 0, 0);
  708. if (IS_ERR(child))
  709. return PTR_ERR(child);
  710. }
  711. if (twl_has_codec() && pdata->audio && twl_class_is_4030()) {
  712. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  713. child = add_child(sub_chip_id, "twl4030-audio",
  714. pdata->audio, sizeof(*pdata->audio),
  715. false, 0, 0);
  716. if (IS_ERR(child))
  717. return PTR_ERR(child);
  718. }
  719. if (twl_has_codec() && pdata->audio && twl_class_is_6030()) {
  720. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  721. child = add_child(sub_chip_id, "twl6040",
  722. pdata->audio, sizeof(*pdata->audio),
  723. false, 0, 0);
  724. if (IS_ERR(child))
  725. return PTR_ERR(child);
  726. }
  727. /* twl4030 regulators */
  728. if (twl_has_regulator() && twl_class_is_4030()) {
  729. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
  730. features);
  731. if (IS_ERR(child))
  732. return PTR_ERR(child);
  733. child = add_regulator(TWL4030_REG_VIO, pdata->vio,
  734. features);
  735. if (IS_ERR(child))
  736. return PTR_ERR(child);
  737. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
  738. features);
  739. if (IS_ERR(child))
  740. return PTR_ERR(child);
  741. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
  742. features);
  743. if (IS_ERR(child))
  744. return PTR_ERR(child);
  745. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
  746. features);
  747. if (IS_ERR(child))
  748. return PTR_ERR(child);
  749. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
  750. features);
  751. if (IS_ERR(child))
  752. return PTR_ERR(child);
  753. child = add_regulator((features & TWL4030_VAUX2)
  754. ? TWL4030_REG_VAUX2_4030
  755. : TWL4030_REG_VAUX2,
  756. pdata->vaux2, features);
  757. if (IS_ERR(child))
  758. return PTR_ERR(child);
  759. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
  760. features);
  761. if (IS_ERR(child))
  762. return PTR_ERR(child);
  763. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
  764. features);
  765. if (IS_ERR(child))
  766. return PTR_ERR(child);
  767. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
  768. features);
  769. if (IS_ERR(child))
  770. return PTR_ERR(child);
  771. }
  772. /* maybe add LDOs that are omitted on cost-reduced parts */
  773. if (twl_has_regulator() && !(features & TPS_SUBSET)
  774. && twl_class_is_4030()) {
  775. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
  776. features);
  777. if (IS_ERR(child))
  778. return PTR_ERR(child);
  779. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
  780. features);
  781. if (IS_ERR(child))
  782. return PTR_ERR(child);
  783. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
  784. features);
  785. if (IS_ERR(child))
  786. return PTR_ERR(child);
  787. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
  788. features);
  789. if (IS_ERR(child))
  790. return PTR_ERR(child);
  791. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
  792. features);
  793. if (IS_ERR(child))
  794. return PTR_ERR(child);
  795. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
  796. features);
  797. if (IS_ERR(child))
  798. return PTR_ERR(child);
  799. }
  800. /* twl6030 regulators */
  801. if (twl_has_regulator() && twl_class_is_6030() &&
  802. !(features & TWL6025_SUBCLASS)) {
  803. child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
  804. features);
  805. if (IS_ERR(child))
  806. return PTR_ERR(child);
  807. child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
  808. features);
  809. if (IS_ERR(child))
  810. return PTR_ERR(child);
  811. child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
  812. features);
  813. if (IS_ERR(child))
  814. return PTR_ERR(child);
  815. child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
  816. features);
  817. if (IS_ERR(child))
  818. return PTR_ERR(child);
  819. child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
  820. features);
  821. if (IS_ERR(child))
  822. return PTR_ERR(child);
  823. child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
  824. features);
  825. if (IS_ERR(child))
  826. return PTR_ERR(child);
  827. child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
  828. features);
  829. if (IS_ERR(child))
  830. return PTR_ERR(child);
  831. child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
  832. features);
  833. if (IS_ERR(child))
  834. return PTR_ERR(child);
  835. child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
  836. features);
  837. if (IS_ERR(child))
  838. return PTR_ERR(child);
  839. }
  840. /* 6030 and 6025 share this regulator */
  841. if (twl_has_regulator() && twl_class_is_6030()) {
  842. child = add_regulator(TWL6030_REG_VANA, pdata->vana,
  843. features);
  844. if (IS_ERR(child))
  845. return PTR_ERR(child);
  846. }
  847. /* twl6025 regulators */
  848. if (twl_has_regulator() && twl_class_is_6030() &&
  849. (features & TWL6025_SUBCLASS)) {
  850. child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
  851. features);
  852. if (IS_ERR(child))
  853. return PTR_ERR(child);
  854. child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
  855. features);
  856. if (IS_ERR(child))
  857. return PTR_ERR(child);
  858. child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
  859. features);
  860. if (IS_ERR(child))
  861. return PTR_ERR(child);
  862. child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
  863. features);
  864. if (IS_ERR(child))
  865. return PTR_ERR(child);
  866. child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
  867. features);
  868. if (IS_ERR(child))
  869. return PTR_ERR(child);
  870. child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
  871. features);
  872. if (IS_ERR(child))
  873. return PTR_ERR(child);
  874. child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
  875. features);
  876. if (IS_ERR(child))
  877. return PTR_ERR(child);
  878. child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
  879. features);
  880. if (IS_ERR(child))
  881. return PTR_ERR(child);
  882. child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
  883. features);
  884. if (IS_ERR(child))
  885. return PTR_ERR(child);
  886. child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
  887. features);
  888. if (IS_ERR(child))
  889. return PTR_ERR(child);
  890. child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
  891. features);
  892. if (IS_ERR(child))
  893. return PTR_ERR(child);
  894. }
  895. if (twl_has_bci() && pdata->bci &&
  896. !(features & (TPS_SUBSET | TWL5031))) {
  897. child = add_child(3, "twl4030_bci",
  898. pdata->bci, sizeof(*pdata->bci), false,
  899. /* irq0 = CHG_PRES, irq1 = BCI */
  900. pdata->irq_base + BCI_PRES_INTR_OFFSET,
  901. pdata->irq_base + BCI_INTR_OFFSET);
  902. if (IS_ERR(child))
  903. return PTR_ERR(child);
  904. }
  905. return 0;
  906. }
  907. /*----------------------------------------------------------------------*/
  908. /*
  909. * These three functions initialize the on-chip clock framework,
  910. * letting it generate the right frequencies for USB, MADC, and
  911. * other purposes.
  912. */
  913. static inline int __init protect_pm_master(void)
  914. {
  915. int e = 0;
  916. e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
  917. TWL4030_PM_MASTER_PROTECT_KEY);
  918. return e;
  919. }
  920. static inline int __init unprotect_pm_master(void)
  921. {
  922. int e = 0;
  923. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  924. TWL4030_PM_MASTER_KEY_CFG1,
  925. TWL4030_PM_MASTER_PROTECT_KEY);
  926. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  927. TWL4030_PM_MASTER_KEY_CFG2,
  928. TWL4030_PM_MASTER_PROTECT_KEY);
  929. return e;
  930. }
  931. static void clocks_init(struct device *dev,
  932. struct twl4030_clock_init_data *clock)
  933. {
  934. int e = 0;
  935. struct clk *osc;
  936. u32 rate;
  937. u8 ctrl = HFCLK_FREQ_26_MHZ;
  938. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  939. if (cpu_is_omap2430())
  940. osc = clk_get(dev, "osc_ck");
  941. else
  942. osc = clk_get(dev, "osc_sys_ck");
  943. if (IS_ERR(osc)) {
  944. printk(KERN_WARNING "Skipping twl internal clock init and "
  945. "using bootloader value (unknown osc rate)\n");
  946. return;
  947. }
  948. rate = clk_get_rate(osc);
  949. clk_put(osc);
  950. #else
  951. /* REVISIT for non-OMAP systems, pass the clock rate from
  952. * board init code, using platform_data.
  953. */
  954. osc = ERR_PTR(-EIO);
  955. printk(KERN_WARNING "Skipping twl internal clock init and "
  956. "using bootloader value (unknown osc rate)\n");
  957. return;
  958. #endif
  959. switch (rate) {
  960. case 19200000:
  961. ctrl = HFCLK_FREQ_19p2_MHZ;
  962. break;
  963. case 26000000:
  964. ctrl = HFCLK_FREQ_26_MHZ;
  965. break;
  966. case 38400000:
  967. ctrl = HFCLK_FREQ_38p4_MHZ;
  968. break;
  969. }
  970. ctrl |= HIGH_PERF_SQ;
  971. if (clock && clock->ck32k_lowpwr_enable)
  972. ctrl |= CK32K_LOWPWR_EN;
  973. e |= unprotect_pm_master();
  974. /* effect->MADC+USB ck en */
  975. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  976. e |= protect_pm_master();
  977. if (e < 0)
  978. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  979. }
  980. /*----------------------------------------------------------------------*/
  981. int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  982. int twl4030_exit_irq(void);
  983. int twl4030_init_chip_irq(const char *chip);
  984. int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  985. int twl6030_exit_irq(void);
  986. static int twl_remove(struct i2c_client *client)
  987. {
  988. unsigned i;
  989. int status;
  990. if (twl_class_is_4030())
  991. status = twl4030_exit_irq();
  992. else
  993. status = twl6030_exit_irq();
  994. if (status < 0)
  995. return status;
  996. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  997. struct twl_client *twl = &twl_modules[i];
  998. if (twl->client && twl->client != client)
  999. i2c_unregister_device(twl->client);
  1000. twl_modules[i].client = NULL;
  1001. }
  1002. inuse = false;
  1003. return 0;
  1004. }
  1005. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  1006. static int __devinit
  1007. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1008. {
  1009. int status;
  1010. unsigned i;
  1011. struct twl4030_platform_data *pdata = client->dev.platform_data;
  1012. struct device_node *node = client->dev.of_node;
  1013. u8 temp;
  1014. int ret = 0;
  1015. int nr_irqs = TWL4030_NR_IRQS;
  1016. if ((id->driver_data) & TWL6030_CLASS)
  1017. nr_irqs = TWL6030_NR_IRQS;
  1018. if (node && !pdata) {
  1019. /*
  1020. * XXX: Temporary pdata until the information is correctly
  1021. * retrieved by every TWL modules from DT.
  1022. */
  1023. pdata = devm_kzalloc(&client->dev,
  1024. sizeof(struct twl4030_platform_data),
  1025. GFP_KERNEL);
  1026. if (!pdata)
  1027. return -ENOMEM;
  1028. }
  1029. if (!pdata) {
  1030. dev_dbg(&client->dev, "no platform data?\n");
  1031. return -EINVAL;
  1032. }
  1033. status = irq_alloc_descs(-1, pdata->irq_base, nr_irqs, 0);
  1034. if (IS_ERR_VALUE(status)) {
  1035. dev_err(&client->dev, "Fail to allocate IRQ descs\n");
  1036. return status;
  1037. }
  1038. pdata->irq_base = status;
  1039. pdata->irq_end = pdata->irq_base + nr_irqs;
  1040. #ifdef CONFIG_IRQ_DOMAIN
  1041. domain.irq_base = pdata->irq_base;
  1042. domain.nr_irq = nr_irqs;
  1043. domain.of_node = of_node_get(node);
  1044. domain.ops = &irq_domain_simple_ops;
  1045. irq_domain_add(&domain);
  1046. #endif
  1047. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  1048. dev_dbg(&client->dev, "can't talk I2C?\n");
  1049. return -EIO;
  1050. }
  1051. if (inuse) {
  1052. dev_dbg(&client->dev, "driver is already in use\n");
  1053. return -EBUSY;
  1054. }
  1055. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  1056. struct twl_client *twl = &twl_modules[i];
  1057. twl->address = client->addr + i;
  1058. if (i == 0)
  1059. twl->client = client;
  1060. else {
  1061. twl->client = i2c_new_dummy(client->adapter,
  1062. twl->address);
  1063. if (!twl->client) {
  1064. dev_err(&client->dev,
  1065. "can't attach client %d\n", i);
  1066. status = -ENOMEM;
  1067. goto fail;
  1068. }
  1069. }
  1070. mutex_init(&twl->xfer_lock);
  1071. }
  1072. inuse = true;
  1073. if ((id->driver_data) & TWL6030_CLASS) {
  1074. twl_id = TWL6030_CLASS_ID;
  1075. twl_map = &twl6030_map[0];
  1076. } else {
  1077. twl_id = TWL4030_CLASS_ID;
  1078. twl_map = &twl4030_map[0];
  1079. }
  1080. /* setup clock framework */
  1081. clocks_init(&client->dev, pdata->clock);
  1082. /* read TWL IDCODE Register */
  1083. if (twl_id == TWL4030_CLASS_ID) {
  1084. ret = twl_read_idcode_register();
  1085. WARN(ret < 0, "Error: reading twl_idcode register value\n");
  1086. }
  1087. /* load power event scripts */
  1088. if (twl_has_power() && pdata->power)
  1089. twl4030_power_init(pdata->power);
  1090. /* Maybe init the T2 Interrupt subsystem */
  1091. if (client->irq
  1092. && pdata->irq_base
  1093. && pdata->irq_end > pdata->irq_base) {
  1094. if (twl_class_is_4030()) {
  1095. twl4030_init_chip_irq(id->name);
  1096. status = twl4030_init_irq(client->irq, pdata->irq_base,
  1097. pdata->irq_end);
  1098. } else {
  1099. status = twl6030_init_irq(client->irq, pdata->irq_base,
  1100. pdata->irq_end);
  1101. }
  1102. if (status < 0)
  1103. goto fail;
  1104. }
  1105. /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  1106. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  1107. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  1108. */
  1109. if (twl_class_is_4030()) {
  1110. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  1111. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  1112. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  1113. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  1114. }
  1115. #ifdef CONFIG_OF_DEVICE
  1116. if (node)
  1117. status = of_platform_populate(node, NULL, NULL, &client->dev);
  1118. else
  1119. #endif
  1120. status = add_children(pdata, id->driver_data);
  1121. fail:
  1122. if (status < 0)
  1123. twl_remove(client);
  1124. return status;
  1125. }
  1126. static const struct i2c_device_id twl_ids[] = {
  1127. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  1128. { "twl5030", 0 }, /* T2 updated */
  1129. { "twl5031", TWL5031 }, /* TWL5030 updated */
  1130. { "tps65950", 0 }, /* catalog version of twl5030 */
  1131. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  1132. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  1133. { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
  1134. and vibrator. Charger in USB module*/
  1135. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  1136. { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
  1137. { /* end of list */ },
  1138. };
  1139. MODULE_DEVICE_TABLE(i2c, twl_ids);
  1140. /* One Client Driver , 4 Clients */
  1141. static struct i2c_driver twl_driver = {
  1142. .driver.name = DRIVER_NAME,
  1143. .id_table = twl_ids,
  1144. .probe = twl_probe,
  1145. .remove = twl_remove,
  1146. };
  1147. static int __init twl_init(void)
  1148. {
  1149. return i2c_add_driver(&twl_driver);
  1150. }
  1151. subsys_initcall(twl_init);
  1152. static void __exit twl_exit(void)
  1153. {
  1154. i2c_del_driver(&twl_driver);
  1155. }
  1156. module_exit(twl_exit);
  1157. MODULE_AUTHOR("Texas Instruments, Inc.");
  1158. MODULE_DESCRIPTION("I2C Core interface for TWL");
  1159. MODULE_LICENSE("GPL");