twl-core.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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. struct twl_regulator_driver_data drv_data;
  536. /* regulator framework demands init_data ... */
  537. if (!pdata)
  538. return NULL;
  539. if (consumers) {
  540. pdata->consumer_supplies = consumers;
  541. pdata->num_consumer_supplies = num_consumers;
  542. }
  543. if (pdata->driver_data) {
  544. /* If we have existing drv_data, just add the flags */
  545. struct twl_regulator_driver_data *tmp;
  546. tmp = pdata->driver_data;
  547. tmp->features |= features;
  548. } else {
  549. /* add new driver data struct, used only during init */
  550. drv_data.features = features;
  551. drv_data.set_voltage = NULL;
  552. drv_data.get_voltage = NULL;
  553. drv_data.data = NULL;
  554. pdata->driver_data = &drv_data;
  555. }
  556. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  557. sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
  558. return add_numbered_child(sub_chip_id, "twl_reg", num,
  559. pdata, sizeof(*pdata), false, 0, 0);
  560. }
  561. static struct device *
  562. add_regulator(int num, struct regulator_init_data *pdata,
  563. unsigned long features)
  564. {
  565. return add_regulator_linked(num, pdata, NULL, 0, features);
  566. }
  567. /*
  568. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  569. * for the PIH, and the next are for the PWR_INT SIH, since
  570. * that's how twl_init_irq() sets things up.
  571. */
  572. static int
  573. add_children(struct twl4030_platform_data *pdata, unsigned long features)
  574. {
  575. struct device *child;
  576. unsigned sub_chip_id;
  577. if (twl_has_gpio() && pdata->gpio) {
  578. child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
  579. pdata->gpio, sizeof(*pdata->gpio),
  580. false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
  581. if (IS_ERR(child))
  582. return PTR_ERR(child);
  583. }
  584. if (twl_has_keypad() && pdata->keypad) {
  585. child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
  586. pdata->keypad, sizeof(*pdata->keypad),
  587. true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
  588. if (IS_ERR(child))
  589. return PTR_ERR(child);
  590. }
  591. if (twl_has_madc() && pdata->madc) {
  592. child = add_child(2, "twl4030_madc",
  593. pdata->madc, sizeof(*pdata->madc),
  594. true, pdata->irq_base + MADC_INTR_OFFSET, 0);
  595. if (IS_ERR(child))
  596. return PTR_ERR(child);
  597. }
  598. if (twl_has_rtc()) {
  599. /*
  600. * REVISIT platform_data here currently might expose the
  601. * "msecure" line ... but for now we just expect board
  602. * setup to tell the chip "it's always ok to SET_TIME".
  603. * Eventually, Linux might become more aware of such
  604. * HW security concerns, and "least privilege".
  605. */
  606. sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
  607. child = add_child(sub_chip_id, "twl_rtc",
  608. NULL, 0,
  609. true, pdata->irq_base + RTC_INTR_OFFSET, 0);
  610. if (IS_ERR(child))
  611. return PTR_ERR(child);
  612. }
  613. if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
  614. static struct regulator_consumer_supply usb1v5 = {
  615. .supply = "usb1v5",
  616. };
  617. static struct regulator_consumer_supply usb1v8 = {
  618. .supply = "usb1v8",
  619. };
  620. static struct regulator_consumer_supply usb3v1 = {
  621. .supply = "usb3v1",
  622. };
  623. /* First add the regulators so that they can be used by transceiver */
  624. if (twl_has_regulator()) {
  625. /* this is a template that gets copied */
  626. struct regulator_init_data usb_fixed = {
  627. .constraints.valid_modes_mask =
  628. REGULATOR_MODE_NORMAL
  629. | REGULATOR_MODE_STANDBY,
  630. .constraints.valid_ops_mask =
  631. REGULATOR_CHANGE_MODE
  632. | REGULATOR_CHANGE_STATUS,
  633. };
  634. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  635. &usb_fixed, &usb1v5, 1,
  636. features);
  637. if (IS_ERR(child))
  638. return PTR_ERR(child);
  639. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  640. &usb_fixed, &usb1v8, 1,
  641. features);
  642. if (IS_ERR(child))
  643. return PTR_ERR(child);
  644. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  645. &usb_fixed, &usb3v1, 1,
  646. features);
  647. if (IS_ERR(child))
  648. return PTR_ERR(child);
  649. }
  650. child = add_child(0, "twl4030_usb",
  651. pdata->usb, sizeof(*pdata->usb),
  652. true,
  653. /* irq0 = USB_PRES, irq1 = USB */
  654. pdata->irq_base + USB_PRES_INTR_OFFSET,
  655. pdata->irq_base + USB_INTR_OFFSET);
  656. if (IS_ERR(child))
  657. return PTR_ERR(child);
  658. /* we need to connect regulators to this transceiver */
  659. if (twl_has_regulator() && child) {
  660. usb1v5.dev_name = dev_name(child);
  661. usb1v8.dev_name = dev_name(child);
  662. usb3v1.dev_name = dev_name(child);
  663. }
  664. }
  665. if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
  666. static struct regulator_consumer_supply usb3v3;
  667. int regulator;
  668. if (twl_has_regulator()) {
  669. /* this is a template that gets copied */
  670. struct regulator_init_data usb_fixed = {
  671. .constraints.valid_modes_mask =
  672. REGULATOR_MODE_NORMAL
  673. | REGULATOR_MODE_STANDBY,
  674. .constraints.valid_ops_mask =
  675. REGULATOR_CHANGE_MODE
  676. | REGULATOR_CHANGE_STATUS,
  677. };
  678. if (features & TWL6025_SUBCLASS) {
  679. usb3v3.supply = "ldousb";
  680. regulator = TWL6025_REG_LDOUSB;
  681. } else {
  682. usb3v3.supply = "vusb";
  683. regulator = TWL6030_REG_VUSB;
  684. }
  685. child = add_regulator_linked(regulator, &usb_fixed,
  686. &usb3v3, 1,
  687. features);
  688. if (IS_ERR(child))
  689. return PTR_ERR(child);
  690. }
  691. pdata->usb->features = features;
  692. child = add_child(0, "twl6030_usb",
  693. pdata->usb, sizeof(*pdata->usb),
  694. true,
  695. /* irq1 = VBUS_PRES, irq0 = USB ID */
  696. pdata->irq_base + USBOTG_INTR_OFFSET,
  697. pdata->irq_base + USB_PRES_INTR_OFFSET);
  698. if (IS_ERR(child))
  699. return PTR_ERR(child);
  700. /* we need to connect regulators to this transceiver */
  701. if (twl_has_regulator() && child)
  702. usb3v3.dev_name = dev_name(child);
  703. } else if (twl_has_regulator() && twl_class_is_6030()) {
  704. if (features & TWL6025_SUBCLASS)
  705. child = add_regulator(TWL6025_REG_LDOUSB,
  706. pdata->ldousb, features);
  707. else
  708. child = add_regulator(TWL6030_REG_VUSB,
  709. pdata->vusb, features);
  710. if (IS_ERR(child))
  711. return PTR_ERR(child);
  712. }
  713. if (twl_has_watchdog() && twl_class_is_4030()) {
  714. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  715. if (IS_ERR(child))
  716. return PTR_ERR(child);
  717. }
  718. if (twl_has_pwrbutton() && twl_class_is_4030()) {
  719. child = add_child(1, "twl4030_pwrbutton",
  720. NULL, 0, true, pdata->irq_base + 8 + 0, 0);
  721. if (IS_ERR(child))
  722. return PTR_ERR(child);
  723. }
  724. if (twl_has_codec() && pdata->audio && twl_class_is_4030()) {
  725. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  726. child = add_child(sub_chip_id, "twl4030-audio",
  727. pdata->audio, sizeof(*pdata->audio),
  728. false, 0, 0);
  729. if (IS_ERR(child))
  730. return PTR_ERR(child);
  731. }
  732. if (twl_has_codec() && pdata->audio && twl_class_is_6030()) {
  733. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  734. child = add_child(sub_chip_id, "twl6040",
  735. pdata->audio, sizeof(*pdata->audio),
  736. false, 0, 0);
  737. if (IS_ERR(child))
  738. return PTR_ERR(child);
  739. }
  740. /* twl4030 regulators */
  741. if (twl_has_regulator() && twl_class_is_4030()) {
  742. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
  743. features);
  744. if (IS_ERR(child))
  745. return PTR_ERR(child);
  746. child = add_regulator(TWL4030_REG_VIO, pdata->vio,
  747. features);
  748. if (IS_ERR(child))
  749. return PTR_ERR(child);
  750. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
  751. features);
  752. if (IS_ERR(child))
  753. return PTR_ERR(child);
  754. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
  755. features);
  756. if (IS_ERR(child))
  757. return PTR_ERR(child);
  758. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
  759. features);
  760. if (IS_ERR(child))
  761. return PTR_ERR(child);
  762. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
  763. features);
  764. if (IS_ERR(child))
  765. return PTR_ERR(child);
  766. child = add_regulator((features & TWL4030_VAUX2)
  767. ? TWL4030_REG_VAUX2_4030
  768. : TWL4030_REG_VAUX2,
  769. pdata->vaux2, features);
  770. if (IS_ERR(child))
  771. return PTR_ERR(child);
  772. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
  773. features);
  774. if (IS_ERR(child))
  775. return PTR_ERR(child);
  776. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
  777. features);
  778. if (IS_ERR(child))
  779. return PTR_ERR(child);
  780. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
  781. features);
  782. if (IS_ERR(child))
  783. return PTR_ERR(child);
  784. }
  785. /* maybe add LDOs that are omitted on cost-reduced parts */
  786. if (twl_has_regulator() && !(features & TPS_SUBSET)
  787. && twl_class_is_4030()) {
  788. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
  789. features);
  790. if (IS_ERR(child))
  791. return PTR_ERR(child);
  792. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
  793. features);
  794. if (IS_ERR(child))
  795. return PTR_ERR(child);
  796. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
  797. features);
  798. if (IS_ERR(child))
  799. return PTR_ERR(child);
  800. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
  801. features);
  802. if (IS_ERR(child))
  803. return PTR_ERR(child);
  804. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
  805. features);
  806. if (IS_ERR(child))
  807. return PTR_ERR(child);
  808. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
  809. features);
  810. if (IS_ERR(child))
  811. return PTR_ERR(child);
  812. }
  813. /* twl6030 regulators */
  814. if (twl_has_regulator() && twl_class_is_6030() &&
  815. !(features & TWL6025_SUBCLASS)) {
  816. child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
  817. features);
  818. if (IS_ERR(child))
  819. return PTR_ERR(child);
  820. child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
  821. features);
  822. if (IS_ERR(child))
  823. return PTR_ERR(child);
  824. child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
  825. features);
  826. if (IS_ERR(child))
  827. return PTR_ERR(child);
  828. child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
  829. features);
  830. if (IS_ERR(child))
  831. return PTR_ERR(child);
  832. child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
  833. features);
  834. if (IS_ERR(child))
  835. return PTR_ERR(child);
  836. child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
  837. features);
  838. if (IS_ERR(child))
  839. return PTR_ERR(child);
  840. child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
  841. features);
  842. if (IS_ERR(child))
  843. return PTR_ERR(child);
  844. child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
  845. features);
  846. if (IS_ERR(child))
  847. return PTR_ERR(child);
  848. child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
  849. features);
  850. if (IS_ERR(child))
  851. return PTR_ERR(child);
  852. child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
  853. features);
  854. if (IS_ERR(child))
  855. return PTR_ERR(child);
  856. child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
  857. features);
  858. if (IS_ERR(child))
  859. return PTR_ERR(child);
  860. child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
  861. features);
  862. if (IS_ERR(child))
  863. return PTR_ERR(child);
  864. child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
  865. features);
  866. if (IS_ERR(child))
  867. return PTR_ERR(child);
  868. child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
  869. features);
  870. if (IS_ERR(child))
  871. return PTR_ERR(child);
  872. }
  873. /* 6030 and 6025 share this regulator */
  874. if (twl_has_regulator() && twl_class_is_6030()) {
  875. child = add_regulator(TWL6030_REG_VANA, pdata->vana,
  876. features);
  877. if (IS_ERR(child))
  878. return PTR_ERR(child);
  879. }
  880. /* twl6025 regulators */
  881. if (twl_has_regulator() && twl_class_is_6030() &&
  882. (features & TWL6025_SUBCLASS)) {
  883. child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
  884. features);
  885. if (IS_ERR(child))
  886. return PTR_ERR(child);
  887. child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
  888. features);
  889. if (IS_ERR(child))
  890. return PTR_ERR(child);
  891. child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
  892. features);
  893. if (IS_ERR(child))
  894. return PTR_ERR(child);
  895. child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
  896. features);
  897. if (IS_ERR(child))
  898. return PTR_ERR(child);
  899. child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
  900. features);
  901. if (IS_ERR(child))
  902. return PTR_ERR(child);
  903. child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
  904. features);
  905. if (IS_ERR(child))
  906. return PTR_ERR(child);
  907. child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
  908. features);
  909. if (IS_ERR(child))
  910. return PTR_ERR(child);
  911. child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
  912. features);
  913. if (IS_ERR(child))
  914. return PTR_ERR(child);
  915. child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
  916. features);
  917. if (IS_ERR(child))
  918. return PTR_ERR(child);
  919. child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
  920. features);
  921. if (IS_ERR(child))
  922. return PTR_ERR(child);
  923. child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
  924. features);
  925. if (IS_ERR(child))
  926. return PTR_ERR(child);
  927. }
  928. if (twl_has_bci() && pdata->bci &&
  929. !(features & (TPS_SUBSET | TWL5031))) {
  930. child = add_child(3, "twl4030_bci",
  931. pdata->bci, sizeof(*pdata->bci), false,
  932. /* irq0 = CHG_PRES, irq1 = BCI */
  933. pdata->irq_base + BCI_PRES_INTR_OFFSET,
  934. pdata->irq_base + BCI_INTR_OFFSET);
  935. if (IS_ERR(child))
  936. return PTR_ERR(child);
  937. }
  938. return 0;
  939. }
  940. /*----------------------------------------------------------------------*/
  941. /*
  942. * These three functions initialize the on-chip clock framework,
  943. * letting it generate the right frequencies for USB, MADC, and
  944. * other purposes.
  945. */
  946. static inline int __init protect_pm_master(void)
  947. {
  948. int e = 0;
  949. e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
  950. TWL4030_PM_MASTER_PROTECT_KEY);
  951. return e;
  952. }
  953. static inline int __init unprotect_pm_master(void)
  954. {
  955. int e = 0;
  956. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  957. TWL4030_PM_MASTER_KEY_CFG1,
  958. TWL4030_PM_MASTER_PROTECT_KEY);
  959. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  960. TWL4030_PM_MASTER_KEY_CFG2,
  961. TWL4030_PM_MASTER_PROTECT_KEY);
  962. return e;
  963. }
  964. static void clocks_init(struct device *dev,
  965. struct twl4030_clock_init_data *clock)
  966. {
  967. int e = 0;
  968. struct clk *osc;
  969. u32 rate;
  970. u8 ctrl = HFCLK_FREQ_26_MHZ;
  971. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  972. if (cpu_is_omap2430())
  973. osc = clk_get(dev, "osc_ck");
  974. else
  975. osc = clk_get(dev, "osc_sys_ck");
  976. if (IS_ERR(osc)) {
  977. printk(KERN_WARNING "Skipping twl internal clock init and "
  978. "using bootloader value (unknown osc rate)\n");
  979. return;
  980. }
  981. rate = clk_get_rate(osc);
  982. clk_put(osc);
  983. #else
  984. /* REVISIT for non-OMAP systems, pass the clock rate from
  985. * board init code, using platform_data.
  986. */
  987. osc = ERR_PTR(-EIO);
  988. printk(KERN_WARNING "Skipping twl internal clock init and "
  989. "using bootloader value (unknown osc rate)\n");
  990. return;
  991. #endif
  992. switch (rate) {
  993. case 19200000:
  994. ctrl = HFCLK_FREQ_19p2_MHZ;
  995. break;
  996. case 26000000:
  997. ctrl = HFCLK_FREQ_26_MHZ;
  998. break;
  999. case 38400000:
  1000. ctrl = HFCLK_FREQ_38p4_MHZ;
  1001. break;
  1002. }
  1003. ctrl |= HIGH_PERF_SQ;
  1004. if (clock && clock->ck32k_lowpwr_enable)
  1005. ctrl |= CK32K_LOWPWR_EN;
  1006. e |= unprotect_pm_master();
  1007. /* effect->MADC+USB ck en */
  1008. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  1009. e |= protect_pm_master();
  1010. if (e < 0)
  1011. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  1012. }
  1013. /*----------------------------------------------------------------------*/
  1014. int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  1015. int twl4030_exit_irq(void);
  1016. int twl4030_init_chip_irq(const char *chip);
  1017. int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  1018. int twl6030_exit_irq(void);
  1019. static int twl_remove(struct i2c_client *client)
  1020. {
  1021. unsigned i;
  1022. int status;
  1023. if (twl_class_is_4030())
  1024. status = twl4030_exit_irq();
  1025. else
  1026. status = twl6030_exit_irq();
  1027. if (status < 0)
  1028. return status;
  1029. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  1030. struct twl_client *twl = &twl_modules[i];
  1031. if (twl->client && twl->client != client)
  1032. i2c_unregister_device(twl->client);
  1033. twl_modules[i].client = NULL;
  1034. }
  1035. inuse = false;
  1036. return 0;
  1037. }
  1038. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  1039. static int __devinit
  1040. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1041. {
  1042. int status;
  1043. unsigned i;
  1044. struct twl4030_platform_data *pdata = client->dev.platform_data;
  1045. struct device_node *node = client->dev.of_node;
  1046. u8 temp;
  1047. int ret = 0;
  1048. int nr_irqs = TWL4030_NR_IRQS;
  1049. if ((id->driver_data) & TWL6030_CLASS)
  1050. nr_irqs = TWL6030_NR_IRQS;
  1051. if (node && !pdata) {
  1052. /*
  1053. * XXX: Temporary pdata until the information is correctly
  1054. * retrieved by every TWL modules from DT.
  1055. */
  1056. pdata = devm_kzalloc(&client->dev,
  1057. sizeof(struct twl4030_platform_data),
  1058. GFP_KERNEL);
  1059. if (!pdata)
  1060. return -ENOMEM;
  1061. }
  1062. if (!pdata) {
  1063. dev_dbg(&client->dev, "no platform data?\n");
  1064. return -EINVAL;
  1065. }
  1066. status = irq_alloc_descs(-1, pdata->irq_base, nr_irqs, 0);
  1067. if (IS_ERR_VALUE(status)) {
  1068. dev_err(&client->dev, "Fail to allocate IRQ descs\n");
  1069. return status;
  1070. }
  1071. pdata->irq_base = status;
  1072. pdata->irq_end = pdata->irq_base + nr_irqs;
  1073. #ifdef CONFIG_IRQ_DOMAIN
  1074. domain.irq_base = pdata->irq_base;
  1075. domain.nr_irq = nr_irqs;
  1076. domain.of_node = of_node_get(node);
  1077. domain.ops = &irq_domain_simple_ops;
  1078. irq_domain_add(&domain);
  1079. #endif
  1080. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  1081. dev_dbg(&client->dev, "can't talk I2C?\n");
  1082. return -EIO;
  1083. }
  1084. if (inuse) {
  1085. dev_dbg(&client->dev, "driver is already in use\n");
  1086. return -EBUSY;
  1087. }
  1088. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  1089. struct twl_client *twl = &twl_modules[i];
  1090. twl->address = client->addr + i;
  1091. if (i == 0)
  1092. twl->client = client;
  1093. else {
  1094. twl->client = i2c_new_dummy(client->adapter,
  1095. twl->address);
  1096. if (!twl->client) {
  1097. dev_err(&client->dev,
  1098. "can't attach client %d\n", i);
  1099. status = -ENOMEM;
  1100. goto fail;
  1101. }
  1102. }
  1103. mutex_init(&twl->xfer_lock);
  1104. }
  1105. inuse = true;
  1106. if ((id->driver_data) & TWL6030_CLASS) {
  1107. twl_id = TWL6030_CLASS_ID;
  1108. twl_map = &twl6030_map[0];
  1109. } else {
  1110. twl_id = TWL4030_CLASS_ID;
  1111. twl_map = &twl4030_map[0];
  1112. }
  1113. /* setup clock framework */
  1114. clocks_init(&client->dev, pdata->clock);
  1115. /* read TWL IDCODE Register */
  1116. if (twl_id == TWL4030_CLASS_ID) {
  1117. ret = twl_read_idcode_register();
  1118. WARN(ret < 0, "Error: reading twl_idcode register value\n");
  1119. }
  1120. /* load power event scripts */
  1121. if (twl_has_power() && pdata->power)
  1122. twl4030_power_init(pdata->power);
  1123. /* Maybe init the T2 Interrupt subsystem */
  1124. if (client->irq
  1125. && pdata->irq_base
  1126. && pdata->irq_end > pdata->irq_base) {
  1127. if (twl_class_is_4030()) {
  1128. twl4030_init_chip_irq(id->name);
  1129. status = twl4030_init_irq(client->irq, pdata->irq_base,
  1130. pdata->irq_end);
  1131. } else {
  1132. status = twl6030_init_irq(client->irq, pdata->irq_base,
  1133. pdata->irq_end);
  1134. }
  1135. if (status < 0)
  1136. goto fail;
  1137. }
  1138. /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  1139. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  1140. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  1141. */
  1142. if (twl_class_is_4030()) {
  1143. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  1144. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  1145. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  1146. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  1147. }
  1148. #ifdef CONFIG_OF_DEVICE
  1149. if (node)
  1150. status = of_platform_populate(node, NULL, NULL, &client->dev);
  1151. else
  1152. #endif
  1153. status = add_children(pdata, id->driver_data);
  1154. fail:
  1155. if (status < 0)
  1156. twl_remove(client);
  1157. return status;
  1158. }
  1159. static const struct i2c_device_id twl_ids[] = {
  1160. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  1161. { "twl5030", 0 }, /* T2 updated */
  1162. { "twl5031", TWL5031 }, /* TWL5030 updated */
  1163. { "tps65950", 0 }, /* catalog version of twl5030 */
  1164. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  1165. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  1166. { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
  1167. and vibrator. Charger in USB module*/
  1168. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  1169. { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
  1170. { /* end of list */ },
  1171. };
  1172. MODULE_DEVICE_TABLE(i2c, twl_ids);
  1173. /* One Client Driver , 4 Clients */
  1174. static struct i2c_driver twl_driver = {
  1175. .driver.name = DRIVER_NAME,
  1176. .id_table = twl_ids,
  1177. .probe = twl_probe,
  1178. .remove = twl_remove,
  1179. };
  1180. static int __init twl_init(void)
  1181. {
  1182. return i2c_add_driver(&twl_driver);
  1183. }
  1184. subsys_initcall(twl_init);
  1185. static void __exit twl_exit(void)
  1186. {
  1187. i2c_del_driver(&twl_driver);
  1188. }
  1189. module_exit(twl_exit);
  1190. MODULE_AUTHOR("Texas Instruments, Inc.");
  1191. MODULE_DESCRIPTION("I2C Core interface for TWL");
  1192. MODULE_LICENSE("GPL");