twl-core.c 35 KB

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