twl-core.c 35 KB

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