twl-core.c 30 KB

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