twl-core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. /* chip-specific feature flags, for i2c_device_id.driver_data */
  191. #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
  192. #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
  193. #define TWL5031 BIT(2) /* twl5031 has different registers */
  194. #define TWL6030_CLASS BIT(3) /* TWL6030 class */
  195. /*----------------------------------------------------------------------*/
  196. /* is driver active, bound to a chip? */
  197. static bool inuse;
  198. /* TWL IDCODE Register value */
  199. static u32 twl_idcode;
  200. static unsigned int twl_id;
  201. unsigned int twl_rev(void)
  202. {
  203. return twl_id;
  204. }
  205. EXPORT_SYMBOL(twl_rev);
  206. /* Structure for each TWL4030/TWL6030 Slave */
  207. struct twl_client {
  208. struct i2c_client *client;
  209. u8 address;
  210. /* max numb of i2c_msg required is for read =2 */
  211. struct i2c_msg xfer_msg[2];
  212. /* To lock access to xfer_msg */
  213. struct mutex xfer_lock;
  214. };
  215. static struct twl_client twl_modules[TWL_NUM_SLAVES];
  216. /* mapping the module id to slave id and base address */
  217. struct twl_mapping {
  218. unsigned char sid; /* Slave ID */
  219. unsigned char base; /* base address */
  220. };
  221. static struct twl_mapping *twl_map;
  222. static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  223. /*
  224. * NOTE: don't change this table without updating the
  225. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  226. * so they continue to match the order in this table.
  227. */
  228. { 0, TWL4030_BASEADD_USB },
  229. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  230. { 1, TWL4030_BASEADD_GPIO },
  231. { 1, TWL4030_BASEADD_INTBR },
  232. { 1, TWL4030_BASEADD_PIH },
  233. { 1, TWL4030_BASEADD_TEST },
  234. { 2, TWL4030_BASEADD_KEYPAD },
  235. { 2, TWL4030_BASEADD_MADC },
  236. { 2, TWL4030_BASEADD_INTERRUPTS },
  237. { 2, TWL4030_BASEADD_LED },
  238. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  239. { 2, TWL4030_BASEADD_PRECHARGE },
  240. { 2, TWL4030_BASEADD_PWM0 },
  241. { 2, TWL4030_BASEADD_PWM1 },
  242. { 2, TWL4030_BASEADD_PWMA },
  243. { 2, TWL4030_BASEADD_PWMB },
  244. { 2, TWL5031_BASEADD_ACCESSORY },
  245. { 2, TWL5031_BASEADD_INTERRUPTS },
  246. { 3, TWL4030_BASEADD_BACKUP },
  247. { 3, TWL4030_BASEADD_INT },
  248. { 3, TWL4030_BASEADD_PM_MASTER },
  249. { 3, TWL4030_BASEADD_PM_RECEIVER },
  250. { 3, TWL4030_BASEADD_RTC },
  251. { 3, TWL4030_BASEADD_SECURED_REG },
  252. };
  253. static struct twl_mapping twl6030_map[] = {
  254. /*
  255. * NOTE: don't change this table without updating the
  256. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  257. * so they continue to match the order in this table.
  258. */
  259. { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
  260. { SUB_CHIP_ID_INVAL, TWL6030_BASEADD_AUDIO },
  261. { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
  262. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  263. { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
  264. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  265. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  266. { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
  267. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  268. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  269. { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
  270. { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
  271. { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
  272. { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
  273. { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
  274. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  275. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  276. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  277. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  278. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  279. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
  280. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
  281. { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
  282. { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
  283. { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
  284. };
  285. /*----------------------------------------------------------------------*/
  286. /* Exported Functions */
  287. /**
  288. * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
  289. * @mod_no: module number
  290. * @value: an array of num_bytes+1 containing data to write
  291. * @reg: register address (just offset will do)
  292. * @num_bytes: number of bytes to transfer
  293. *
  294. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  295. * valid data starts at Offset 1.
  296. *
  297. * Returns the result of operation - 0 is success
  298. */
  299. int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  300. {
  301. int ret;
  302. int sid;
  303. struct twl_client *twl;
  304. struct i2c_msg *msg;
  305. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  306. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  307. return -EPERM;
  308. }
  309. if (unlikely(!inuse)) {
  310. pr_err("%s: not initialized\n", DRIVER_NAME);
  311. return -EPERM;
  312. }
  313. sid = twl_map[mod_no].sid;
  314. if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
  315. pr_err("%s: module %d is not part of the pmic\n",
  316. DRIVER_NAME, mod_no);
  317. return -EINVAL;
  318. }
  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. if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
  373. pr_err("%s: module %d is not part of the pmic\n",
  374. DRIVER_NAME, mod_no);
  375. return -EINVAL;
  376. }
  377. twl = &twl_modules[sid];
  378. mutex_lock(&twl->xfer_lock);
  379. /* [MSG1] fill the register address data */
  380. msg = &twl->xfer_msg[0];
  381. msg->addr = twl->address;
  382. msg->len = 1;
  383. msg->flags = 0; /* Read the register value */
  384. val = twl_map[mod_no].base + reg;
  385. msg->buf = &val;
  386. /* [MSG2] fill the data rx buffer */
  387. msg = &twl->xfer_msg[1];
  388. msg->addr = twl->address;
  389. msg->flags = I2C_M_RD; /* Read the register value */
  390. msg->len = num_bytes; /* only n bytes */
  391. msg->buf = value;
  392. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  393. mutex_unlock(&twl->xfer_lock);
  394. /* i2c_transfer returns number of messages transferred */
  395. if (ret != 2) {
  396. pr_err("%s: i2c_read failed to transfer all messages\n",
  397. DRIVER_NAME);
  398. if (ret < 0)
  399. return ret;
  400. else
  401. return -EIO;
  402. } else {
  403. return 0;
  404. }
  405. }
  406. EXPORT_SYMBOL(twl_i2c_read);
  407. /**
  408. * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
  409. * @mod_no: module number
  410. * @value: the value to be written 8 bit
  411. * @reg: register address (just offset will do)
  412. *
  413. * Returns result of operation - 0 is success
  414. */
  415. int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  416. {
  417. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  418. u8 temp_buffer[2] = { 0 };
  419. /* offset 1 contains the data */
  420. temp_buffer[1] = value;
  421. return twl_i2c_write(mod_no, temp_buffer, reg, 1);
  422. }
  423. EXPORT_SYMBOL(twl_i2c_write_u8);
  424. /**
  425. * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
  426. * @mod_no: module number
  427. * @value: the value read 8 bit
  428. * @reg: register address (just offset will do)
  429. *
  430. * Returns result of operation - 0 is success
  431. */
  432. int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  433. {
  434. return twl_i2c_read(mod_no, value, reg, 1);
  435. }
  436. EXPORT_SYMBOL(twl_i2c_read_u8);
  437. /*----------------------------------------------------------------------*/
  438. /**
  439. * twl_read_idcode_register - API to read the IDCODE register.
  440. *
  441. * Unlocks the IDCODE register and read the 32 bit value.
  442. */
  443. static int twl_read_idcode_register(void)
  444. {
  445. int err;
  446. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
  447. REG_UNLOCK_TEST_REG);
  448. if (err) {
  449. pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
  450. goto fail;
  451. }
  452. err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
  453. REG_IDCODE_7_0, 4);
  454. if (err) {
  455. pr_err("TWL4030: unable to read IDCODE -%d\n", err);
  456. goto fail;
  457. }
  458. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
  459. if (err)
  460. pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
  461. fail:
  462. return err;
  463. }
  464. /**
  465. * twl_get_type - API to get TWL Si type.
  466. *
  467. * Api to get the TWL Si type from IDCODE value.
  468. */
  469. int twl_get_type(void)
  470. {
  471. return TWL_SIL_TYPE(twl_idcode);
  472. }
  473. EXPORT_SYMBOL_GPL(twl_get_type);
  474. /**
  475. * twl_get_version - API to get TWL Si version.
  476. *
  477. * Api to get the TWL Si version from IDCODE value.
  478. */
  479. int twl_get_version(void)
  480. {
  481. return TWL_SIL_REV(twl_idcode);
  482. }
  483. EXPORT_SYMBOL_GPL(twl_get_version);
  484. static struct device *
  485. add_numbered_child(unsigned chip, const char *name, int num,
  486. void *pdata, unsigned pdata_len,
  487. bool can_wakeup, int irq0, int irq1)
  488. {
  489. struct platform_device *pdev;
  490. struct twl_client *twl = &twl_modules[chip];
  491. int status;
  492. pdev = platform_device_alloc(name, num);
  493. if (!pdev) {
  494. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  495. status = -ENOMEM;
  496. goto err;
  497. }
  498. device_init_wakeup(&pdev->dev, can_wakeup);
  499. pdev->dev.parent = &twl->client->dev;
  500. if (pdata) {
  501. status = platform_device_add_data(pdev, pdata, pdata_len);
  502. if (status < 0) {
  503. dev_dbg(&pdev->dev, "can't add platform_data\n");
  504. goto err;
  505. }
  506. }
  507. if (irq0) {
  508. struct resource r[2] = {
  509. { .start = irq0, .flags = IORESOURCE_IRQ, },
  510. { .start = irq1, .flags = IORESOURCE_IRQ, },
  511. };
  512. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  513. if (status < 0) {
  514. dev_dbg(&pdev->dev, "can't add irqs\n");
  515. goto err;
  516. }
  517. }
  518. status = platform_device_add(pdev);
  519. err:
  520. if (status < 0) {
  521. platform_device_put(pdev);
  522. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  523. return ERR_PTR(status);
  524. }
  525. return &pdev->dev;
  526. }
  527. static inline struct device *add_child(unsigned chip, const char *name,
  528. void *pdata, unsigned pdata_len,
  529. bool can_wakeup, int irq0, int irq1)
  530. {
  531. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  532. can_wakeup, irq0, irq1);
  533. }
  534. static struct device *
  535. add_regulator_linked(int num, struct regulator_init_data *pdata,
  536. struct regulator_consumer_supply *consumers,
  537. unsigned num_consumers, unsigned long features)
  538. {
  539. unsigned sub_chip_id;
  540. struct twl_regulator_driver_data drv_data;
  541. /* regulator framework demands init_data ... */
  542. if (!pdata)
  543. return NULL;
  544. if (consumers) {
  545. pdata->consumer_supplies = consumers;
  546. pdata->num_consumer_supplies = num_consumers;
  547. }
  548. if (pdata->driver_data) {
  549. /* If we have existing drv_data, just add the flags */
  550. struct twl_regulator_driver_data *tmp;
  551. tmp = pdata->driver_data;
  552. tmp->features |= features;
  553. } else {
  554. /* add new driver data struct, used only during init */
  555. drv_data.features = features;
  556. drv_data.set_voltage = NULL;
  557. drv_data.get_voltage = NULL;
  558. drv_data.data = NULL;
  559. pdata->driver_data = &drv_data;
  560. }
  561. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  562. sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
  563. return add_numbered_child(sub_chip_id, "twl_reg", num,
  564. pdata, sizeof(*pdata), false, 0, 0);
  565. }
  566. static struct device *
  567. add_regulator(int num, struct regulator_init_data *pdata,
  568. unsigned long features)
  569. {
  570. return add_regulator_linked(num, pdata, NULL, 0, features);
  571. }
  572. /*
  573. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  574. * for the PIH, and the next are for the PWR_INT SIH, since
  575. * that's how twl_init_irq() sets things up.
  576. */
  577. static int
  578. add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
  579. unsigned long features)
  580. {
  581. struct device *child;
  582. unsigned sub_chip_id;
  583. if (twl_has_gpio() && pdata->gpio) {
  584. child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
  585. pdata->gpio, sizeof(*pdata->gpio),
  586. false, irq_base + GPIO_INTR_OFFSET, 0);
  587. if (IS_ERR(child))
  588. return PTR_ERR(child);
  589. }
  590. if (twl_has_keypad() && pdata->keypad) {
  591. child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
  592. pdata->keypad, sizeof(*pdata->keypad),
  593. true, irq_base + KEYPAD_INTR_OFFSET, 0);
  594. if (IS_ERR(child))
  595. return PTR_ERR(child);
  596. }
  597. if (twl_has_madc() && pdata->madc) {
  598. child = add_child(2, "twl4030_madc",
  599. pdata->madc, sizeof(*pdata->madc),
  600. true, irq_base + MADC_INTR_OFFSET, 0);
  601. if (IS_ERR(child))
  602. return PTR_ERR(child);
  603. }
  604. if (twl_has_rtc()) {
  605. /*
  606. * REVISIT platform_data here currently might expose the
  607. * "msecure" line ... but for now we just expect board
  608. * setup to tell the chip "it's always ok to SET_TIME".
  609. * Eventually, Linux might become more aware of such
  610. * HW security concerns, and "least privilege".
  611. */
  612. sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
  613. child = add_child(sub_chip_id, "twl_rtc",
  614. NULL, 0,
  615. true, irq_base + RTC_INTR_OFFSET, 0);
  616. if (IS_ERR(child))
  617. return PTR_ERR(child);
  618. }
  619. if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
  620. static struct regulator_consumer_supply usb1v5 = {
  621. .supply = "usb1v5",
  622. };
  623. static struct regulator_consumer_supply usb1v8 = {
  624. .supply = "usb1v8",
  625. };
  626. static struct regulator_consumer_supply usb3v1 = {
  627. .supply = "usb3v1",
  628. };
  629. /* First add the regulators so that they can be used by transceiver */
  630. if (twl_has_regulator()) {
  631. /* this is a template that gets copied */
  632. struct regulator_init_data usb_fixed = {
  633. .constraints.valid_modes_mask =
  634. REGULATOR_MODE_NORMAL
  635. | REGULATOR_MODE_STANDBY,
  636. .constraints.valid_ops_mask =
  637. REGULATOR_CHANGE_MODE
  638. | REGULATOR_CHANGE_STATUS,
  639. };
  640. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  641. &usb_fixed, &usb1v5, 1,
  642. features);
  643. if (IS_ERR(child))
  644. return PTR_ERR(child);
  645. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  646. &usb_fixed, &usb1v8, 1,
  647. features);
  648. if (IS_ERR(child))
  649. return PTR_ERR(child);
  650. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  651. &usb_fixed, &usb3v1, 1,
  652. features);
  653. if (IS_ERR(child))
  654. return PTR_ERR(child);
  655. }
  656. child = add_child(0, "twl4030_usb",
  657. pdata->usb, sizeof(*pdata->usb),
  658. true,
  659. /* irq0 = USB_PRES, irq1 = USB */
  660. irq_base + USB_PRES_INTR_OFFSET,
  661. irq_base + USB_INTR_OFFSET);
  662. if (IS_ERR(child))
  663. return PTR_ERR(child);
  664. /* we need to connect regulators to this transceiver */
  665. if (twl_has_regulator() && child) {
  666. usb1v5.dev_name = dev_name(child);
  667. usb1v8.dev_name = dev_name(child);
  668. usb3v1.dev_name = dev_name(child);
  669. }
  670. }
  671. if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
  672. static struct regulator_consumer_supply usb3v3;
  673. int regulator;
  674. if (twl_has_regulator()) {
  675. /* this is a template that gets copied */
  676. struct regulator_init_data usb_fixed = {
  677. .constraints.valid_modes_mask =
  678. REGULATOR_MODE_NORMAL
  679. | REGULATOR_MODE_STANDBY,
  680. .constraints.valid_ops_mask =
  681. REGULATOR_CHANGE_MODE
  682. | REGULATOR_CHANGE_STATUS,
  683. };
  684. if (features & TWL6025_SUBCLASS) {
  685. usb3v3.supply = "ldousb";
  686. regulator = TWL6025_REG_LDOUSB;
  687. } else {
  688. usb3v3.supply = "vusb";
  689. regulator = TWL6030_REG_VUSB;
  690. }
  691. child = add_regulator_linked(regulator, &usb_fixed,
  692. &usb3v3, 1,
  693. features);
  694. if (IS_ERR(child))
  695. return PTR_ERR(child);
  696. }
  697. pdata->usb->features = features;
  698. child = add_child(0, "twl6030_usb",
  699. pdata->usb, sizeof(*pdata->usb),
  700. true,
  701. /* irq1 = VBUS_PRES, irq0 = USB ID */
  702. irq_base + USBOTG_INTR_OFFSET,
  703. irq_base + USB_PRES_INTR_OFFSET);
  704. if (IS_ERR(child))
  705. return PTR_ERR(child);
  706. /* we need to connect regulators to this transceiver */
  707. if (twl_has_regulator() && child)
  708. usb3v3.dev_name = dev_name(child);
  709. } else if (twl_has_regulator() && twl_class_is_6030()) {
  710. if (features & TWL6025_SUBCLASS)
  711. child = add_regulator(TWL6025_REG_LDOUSB,
  712. pdata->ldousb, features);
  713. else
  714. child = add_regulator(TWL6030_REG_VUSB,
  715. pdata->vusb, features);
  716. if (IS_ERR(child))
  717. return PTR_ERR(child);
  718. }
  719. if (twl_has_watchdog() && twl_class_is_4030()) {
  720. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  721. if (IS_ERR(child))
  722. return PTR_ERR(child);
  723. }
  724. if (twl_has_pwrbutton() && twl_class_is_4030()) {
  725. child = add_child(1, "twl4030_pwrbutton",
  726. NULL, 0, true, irq_base + 8 + 0, 0);
  727. if (IS_ERR(child))
  728. return PTR_ERR(child);
  729. }
  730. if (twl_has_codec() && pdata->audio && twl_class_is_4030()) {
  731. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  732. child = add_child(sub_chip_id, "twl4030-audio",
  733. pdata->audio, sizeof(*pdata->audio),
  734. false, 0, 0);
  735. if (IS_ERR(child))
  736. return PTR_ERR(child);
  737. }
  738. /* twl4030 regulators */
  739. if (twl_has_regulator() && twl_class_is_4030()) {
  740. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
  741. features);
  742. if (IS_ERR(child))
  743. return PTR_ERR(child);
  744. child = add_regulator(TWL4030_REG_VIO, pdata->vio,
  745. features);
  746. if (IS_ERR(child))
  747. return PTR_ERR(child);
  748. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
  749. features);
  750. if (IS_ERR(child))
  751. return PTR_ERR(child);
  752. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
  753. features);
  754. if (IS_ERR(child))
  755. return PTR_ERR(child);
  756. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
  757. features);
  758. if (IS_ERR(child))
  759. return PTR_ERR(child);
  760. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
  761. features);
  762. if (IS_ERR(child))
  763. return PTR_ERR(child);
  764. child = add_regulator((features & TWL4030_VAUX2)
  765. ? TWL4030_REG_VAUX2_4030
  766. : TWL4030_REG_VAUX2,
  767. pdata->vaux2, features);
  768. if (IS_ERR(child))
  769. return PTR_ERR(child);
  770. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
  771. features);
  772. if (IS_ERR(child))
  773. return PTR_ERR(child);
  774. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
  775. features);
  776. if (IS_ERR(child))
  777. return PTR_ERR(child);
  778. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
  779. features);
  780. if (IS_ERR(child))
  781. return PTR_ERR(child);
  782. }
  783. /* maybe add LDOs that are omitted on cost-reduced parts */
  784. if (twl_has_regulator() && !(features & TPS_SUBSET)
  785. && twl_class_is_4030()) {
  786. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
  787. features);
  788. if (IS_ERR(child))
  789. return PTR_ERR(child);
  790. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
  791. features);
  792. if (IS_ERR(child))
  793. return PTR_ERR(child);
  794. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
  795. features);
  796. if (IS_ERR(child))
  797. return PTR_ERR(child);
  798. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
  799. features);
  800. if (IS_ERR(child))
  801. return PTR_ERR(child);
  802. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
  803. features);
  804. if (IS_ERR(child))
  805. return PTR_ERR(child);
  806. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
  807. features);
  808. if (IS_ERR(child))
  809. return PTR_ERR(child);
  810. }
  811. /* twl6030 regulators */
  812. if (twl_has_regulator() && twl_class_is_6030() &&
  813. !(features & TWL6025_SUBCLASS)) {
  814. child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
  815. features);
  816. if (IS_ERR(child))
  817. return PTR_ERR(child);
  818. child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
  819. features);
  820. if (IS_ERR(child))
  821. return PTR_ERR(child);
  822. child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
  823. features);
  824. if (IS_ERR(child))
  825. return PTR_ERR(child);
  826. child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
  827. features);
  828. if (IS_ERR(child))
  829. return PTR_ERR(child);
  830. child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
  831. features);
  832. if (IS_ERR(child))
  833. return PTR_ERR(child);
  834. child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
  835. features);
  836. if (IS_ERR(child))
  837. return PTR_ERR(child);
  838. child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
  839. features);
  840. if (IS_ERR(child))
  841. return PTR_ERR(child);
  842. child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
  843. features);
  844. if (IS_ERR(child))
  845. return PTR_ERR(child);
  846. child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
  847. features);
  848. if (IS_ERR(child))
  849. return PTR_ERR(child);
  850. child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
  851. features);
  852. if (IS_ERR(child))
  853. return PTR_ERR(child);
  854. child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
  855. features);
  856. if (IS_ERR(child))
  857. return PTR_ERR(child);
  858. child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
  859. features);
  860. if (IS_ERR(child))
  861. return PTR_ERR(child);
  862. child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
  863. features);
  864. if (IS_ERR(child))
  865. return PTR_ERR(child);
  866. child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
  867. features);
  868. if (IS_ERR(child))
  869. return PTR_ERR(child);
  870. }
  871. /* 6030 and 6025 share this regulator */
  872. if (twl_has_regulator() && twl_class_is_6030()) {
  873. child = add_regulator(TWL6030_REG_VANA, pdata->vana,
  874. features);
  875. if (IS_ERR(child))
  876. return PTR_ERR(child);
  877. }
  878. /* twl6025 regulators */
  879. if (twl_has_regulator() && twl_class_is_6030() &&
  880. (features & TWL6025_SUBCLASS)) {
  881. child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
  882. features);
  883. if (IS_ERR(child))
  884. return PTR_ERR(child);
  885. child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
  886. features);
  887. if (IS_ERR(child))
  888. return PTR_ERR(child);
  889. child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
  890. features);
  891. if (IS_ERR(child))
  892. return PTR_ERR(child);
  893. child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
  894. features);
  895. if (IS_ERR(child))
  896. return PTR_ERR(child);
  897. child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
  898. features);
  899. if (IS_ERR(child))
  900. return PTR_ERR(child);
  901. child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
  902. features);
  903. if (IS_ERR(child))
  904. return PTR_ERR(child);
  905. child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
  906. features);
  907. if (IS_ERR(child))
  908. return PTR_ERR(child);
  909. child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
  910. features);
  911. if (IS_ERR(child))
  912. return PTR_ERR(child);
  913. child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
  914. features);
  915. if (IS_ERR(child))
  916. return PTR_ERR(child);
  917. child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
  918. features);
  919. if (IS_ERR(child))
  920. return PTR_ERR(child);
  921. child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
  922. features);
  923. if (IS_ERR(child))
  924. return PTR_ERR(child);
  925. }
  926. if (twl_has_bci() && pdata->bci &&
  927. !(features & (TPS_SUBSET | TWL5031))) {
  928. child = add_child(3, "twl4030_bci",
  929. pdata->bci, sizeof(*pdata->bci), false,
  930. /* irq0 = CHG_PRES, irq1 = BCI */
  931. irq_base + BCI_PRES_INTR_OFFSET,
  932. irq_base + BCI_INTR_OFFSET);
  933. if (IS_ERR(child))
  934. return PTR_ERR(child);
  935. }
  936. return 0;
  937. }
  938. /*----------------------------------------------------------------------*/
  939. /*
  940. * These three functions initialize the on-chip clock framework,
  941. * letting it generate the right frequencies for USB, MADC, and
  942. * other purposes.
  943. */
  944. static inline int __init protect_pm_master(void)
  945. {
  946. int e = 0;
  947. e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
  948. TWL4030_PM_MASTER_PROTECT_KEY);
  949. return e;
  950. }
  951. static inline int __init unprotect_pm_master(void)
  952. {
  953. int e = 0;
  954. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  955. TWL4030_PM_MASTER_KEY_CFG1,
  956. TWL4030_PM_MASTER_PROTECT_KEY);
  957. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  958. TWL4030_PM_MASTER_KEY_CFG2,
  959. TWL4030_PM_MASTER_PROTECT_KEY);
  960. return e;
  961. }
  962. static void clocks_init(struct device *dev,
  963. struct twl4030_clock_init_data *clock)
  964. {
  965. int e = 0;
  966. struct clk *osc;
  967. u32 rate;
  968. u8 ctrl = HFCLK_FREQ_26_MHZ;
  969. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  970. if (cpu_is_omap2430())
  971. osc = clk_get(dev, "osc_ck");
  972. else
  973. osc = clk_get(dev, "osc_sys_ck");
  974. if (IS_ERR(osc)) {
  975. printk(KERN_WARNING "Skipping twl internal clock init and "
  976. "using bootloader value (unknown osc rate)\n");
  977. return;
  978. }
  979. rate = clk_get_rate(osc);
  980. clk_put(osc);
  981. #else
  982. /* REVISIT for non-OMAP systems, pass the clock rate from
  983. * board init code, using platform_data.
  984. */
  985. osc = ERR_PTR(-EIO);
  986. printk(KERN_WARNING "Skipping twl internal clock init and "
  987. "using bootloader value (unknown osc rate)\n");
  988. return;
  989. #endif
  990. switch (rate) {
  991. case 19200000:
  992. ctrl = HFCLK_FREQ_19p2_MHZ;
  993. break;
  994. case 26000000:
  995. ctrl = HFCLK_FREQ_26_MHZ;
  996. break;
  997. case 38400000:
  998. ctrl = HFCLK_FREQ_38p4_MHZ;
  999. break;
  1000. }
  1001. ctrl |= HIGH_PERF_SQ;
  1002. if (clock && clock->ck32k_lowpwr_enable)
  1003. ctrl |= CK32K_LOWPWR_EN;
  1004. e |= unprotect_pm_master();
  1005. /* effect->MADC+USB ck en */
  1006. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  1007. e |= protect_pm_master();
  1008. if (e < 0)
  1009. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  1010. }
  1011. /*----------------------------------------------------------------------*/
  1012. static int twl_remove(struct i2c_client *client)
  1013. {
  1014. unsigned i, num_slaves;
  1015. int status;
  1016. if (twl_class_is_4030()) {
  1017. status = twl4030_exit_irq();
  1018. num_slaves = TWL_NUM_SLAVES;
  1019. } else {
  1020. status = twl6030_exit_irq();
  1021. num_slaves = TWL_NUM_SLAVES - 1;
  1022. }
  1023. if (status < 0)
  1024. return status;
  1025. for (i = 0; i < num_slaves; i++) {
  1026. struct twl_client *twl = &twl_modules[i];
  1027. if (twl->client && twl->client != client)
  1028. i2c_unregister_device(twl->client);
  1029. twl_modules[i].client = NULL;
  1030. }
  1031. inuse = false;
  1032. return 0;
  1033. }
  1034. /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
  1035. static int __devinit
  1036. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1037. {
  1038. struct twl4030_platform_data *pdata = client->dev.platform_data;
  1039. struct device_node *node = client->dev.of_node;
  1040. int irq_base = 0;
  1041. int status;
  1042. unsigned i, num_slaves;
  1043. if (node && !pdata) {
  1044. /*
  1045. * XXX: Temporary pdata until the information is correctly
  1046. * retrieved by every TWL modules from DT.
  1047. */
  1048. pdata = devm_kzalloc(&client->dev,
  1049. sizeof(struct twl4030_platform_data),
  1050. GFP_KERNEL);
  1051. if (!pdata)
  1052. return -ENOMEM;
  1053. }
  1054. if (!pdata) {
  1055. dev_dbg(&client->dev, "no platform data?\n");
  1056. return -EINVAL;
  1057. }
  1058. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  1059. dev_dbg(&client->dev, "can't talk I2C?\n");
  1060. return -EIO;
  1061. }
  1062. if (inuse) {
  1063. dev_dbg(&client->dev, "driver is already in use\n");
  1064. return -EBUSY;
  1065. }
  1066. if ((id->driver_data) & TWL6030_CLASS) {
  1067. twl_id = TWL6030_CLASS_ID;
  1068. twl_map = &twl6030_map[0];
  1069. num_slaves = TWL_NUM_SLAVES - 1;
  1070. } else {
  1071. twl_id = TWL4030_CLASS_ID;
  1072. twl_map = &twl4030_map[0];
  1073. num_slaves = TWL_NUM_SLAVES;
  1074. }
  1075. for (i = 0; i < num_slaves; i++) {
  1076. struct twl_client *twl = &twl_modules[i];
  1077. twl->address = client->addr + i;
  1078. if (i == 0) {
  1079. twl->client = client;
  1080. } else {
  1081. twl->client = i2c_new_dummy(client->adapter,
  1082. twl->address);
  1083. if (!twl->client) {
  1084. dev_err(&client->dev,
  1085. "can't attach client %d\n", i);
  1086. status = -ENOMEM;
  1087. goto fail;
  1088. }
  1089. }
  1090. mutex_init(&twl->xfer_lock);
  1091. }
  1092. inuse = true;
  1093. /* setup clock framework */
  1094. clocks_init(&client->dev, pdata->clock);
  1095. /* read TWL IDCODE Register */
  1096. if (twl_id == TWL4030_CLASS_ID) {
  1097. status = twl_read_idcode_register();
  1098. WARN(status < 0, "Error: reading twl_idcode register value\n");
  1099. }
  1100. /* load power event scripts */
  1101. if (twl_has_power() && pdata->power)
  1102. twl4030_power_init(pdata->power);
  1103. /* Maybe init the T2 Interrupt subsystem */
  1104. if (client->irq) {
  1105. if (twl_class_is_4030()) {
  1106. twl4030_init_chip_irq(id->name);
  1107. irq_base = twl4030_init_irq(&client->dev, client->irq);
  1108. } else {
  1109. irq_base = twl6030_init_irq(&client->dev, client->irq);
  1110. }
  1111. if (irq_base < 0) {
  1112. status = irq_base;
  1113. goto fail;
  1114. }
  1115. }
  1116. /*
  1117. * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  1118. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  1119. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  1120. */
  1121. if (twl_class_is_4030()) {
  1122. u8 temp;
  1123. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  1124. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  1125. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  1126. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  1127. }
  1128. status = -ENODEV;
  1129. if (node)
  1130. status = of_platform_populate(node, NULL, NULL, &client->dev);
  1131. if (status)
  1132. status = add_children(pdata, irq_base, id->driver_data);
  1133. fail:
  1134. if (status < 0)
  1135. twl_remove(client);
  1136. return status;
  1137. }
  1138. static const struct i2c_device_id twl_ids[] = {
  1139. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  1140. { "twl5030", 0 }, /* T2 updated */
  1141. { "twl5031", TWL5031 }, /* TWL5030 updated */
  1142. { "tps65950", 0 }, /* catalog version of twl5030 */
  1143. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  1144. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  1145. { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
  1146. and vibrator. Charger in USB module*/
  1147. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  1148. { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
  1149. { /* end of list */ },
  1150. };
  1151. MODULE_DEVICE_TABLE(i2c, twl_ids);
  1152. /* One Client Driver , 4 Clients */
  1153. static struct i2c_driver twl_driver = {
  1154. .driver.name = DRIVER_NAME,
  1155. .id_table = twl_ids,
  1156. .probe = twl_probe,
  1157. .remove = twl_remove,
  1158. };
  1159. static int __init twl_init(void)
  1160. {
  1161. return i2c_add_driver(&twl_driver);
  1162. }
  1163. subsys_initcall(twl_init);
  1164. static void __exit twl_exit(void)
  1165. {
  1166. i2c_del_driver(&twl_driver);
  1167. }
  1168. module_exit(twl_exit);
  1169. MODULE_AUTHOR("Texas Instruments, Inc.");
  1170. MODULE_DESCRIPTION("I2C Core interface for TWL");
  1171. MODULE_LICENSE("GPL");