twl-core.c 34 KB

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