twl-core.c 35 KB

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