low_i2c.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * arch/powerpc/platforms/powermac/low_i2c.c
  3. *
  4. * Copyright (C) 2003-2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * The linux i2c layer isn't completely suitable for our needs for various
  12. * reasons ranging from too late initialisation to semantics not perfectly
  13. * matching some requirements of the apple platform functions etc...
  14. *
  15. * This file thus provides a simple low level unified i2c interface for
  16. * powermac that covers the various types of i2c busses used in Apple machines.
  17. * For now, keywest, PMU and SMU, though we could add Cuda, or other bit
  18. * banging busses found on older chipstes in earlier machines if we ever need
  19. * one of them.
  20. *
  21. * The drivers in this file are synchronous/blocking. In addition, the
  22. * keywest one is fairly slow due to the use of msleep instead of interrupts
  23. * as the interrupt is currently used by i2c-keywest. In the long run, we
  24. * might want to get rid of those high-level interfaces to linux i2c layer
  25. * either completely (converting all drivers) or replacing them all with a
  26. * single stub driver on top of this one. Once done, the interrupt will be
  27. * available for our use.
  28. */
  29. #undef DEBUG
  30. #undef DEBUG_LOW
  31. #include <linux/types.h>
  32. #include <linux/sched.h>
  33. #include <linux/init.h>
  34. #include <linux/module.h>
  35. #include <linux/adb.h>
  36. #include <linux/pmu.h>
  37. #include <linux/delay.h>
  38. #include <linux/completion.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/timer.h>
  42. #include <linux/mutex.h>
  43. #include <asm/keylargo.h>
  44. #include <asm/uninorth.h>
  45. #include <asm/io.h>
  46. #include <asm/prom.h>
  47. #include <asm/machdep.h>
  48. #include <asm/smu.h>
  49. #include <asm/pmac_pfunc.h>
  50. #include <asm/pmac_low_i2c.h>
  51. #ifdef DEBUG
  52. #define DBG(x...) do {\
  53. printk(KERN_DEBUG "low_i2c:" x); \
  54. } while(0)
  55. #else
  56. #define DBG(x...)
  57. #endif
  58. #ifdef DEBUG_LOW
  59. #define DBG_LOW(x...) do {\
  60. printk(KERN_DEBUG "low_i2c:" x); \
  61. } while(0)
  62. #else
  63. #define DBG_LOW(x...)
  64. #endif
  65. static int pmac_i2c_force_poll = 1;
  66. /*
  67. * A bus structure. Each bus in the system has such a structure associated.
  68. */
  69. struct pmac_i2c_bus
  70. {
  71. struct list_head link;
  72. struct device_node *controller;
  73. struct device_node *busnode;
  74. int type;
  75. int flags;
  76. struct i2c_adapter *adapter;
  77. void *hostdata;
  78. int channel; /* some hosts have multiple */
  79. int mode; /* current mode */
  80. struct mutex mutex;
  81. int opened;
  82. int polled; /* open mode */
  83. struct platform_device *platform_dev;
  84. /* ops */
  85. int (*open)(struct pmac_i2c_bus *bus);
  86. void (*close)(struct pmac_i2c_bus *bus);
  87. int (*xfer)(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  88. u32 subaddr, u8 *data, int len);
  89. };
  90. static LIST_HEAD(pmac_i2c_busses);
  91. /*
  92. * Keywest implementation
  93. */
  94. struct pmac_i2c_host_kw
  95. {
  96. struct mutex mutex; /* Access mutex for use by
  97. * i2c-keywest */
  98. void __iomem *base; /* register base address */
  99. int bsteps; /* register stepping */
  100. int speed; /* speed */
  101. int irq;
  102. u8 *data;
  103. unsigned len;
  104. int state;
  105. int rw;
  106. int polled;
  107. int result;
  108. struct completion complete;
  109. spinlock_t lock;
  110. struct timer_list timeout_timer;
  111. };
  112. /* Register indices */
  113. typedef enum {
  114. reg_mode = 0,
  115. reg_control,
  116. reg_status,
  117. reg_isr,
  118. reg_ier,
  119. reg_addr,
  120. reg_subaddr,
  121. reg_data
  122. } reg_t;
  123. /* The Tumbler audio equalizer can be really slow sometimes */
  124. #define KW_POLL_TIMEOUT (2*HZ)
  125. /* Mode register */
  126. #define KW_I2C_MODE_100KHZ 0x00
  127. #define KW_I2C_MODE_50KHZ 0x01
  128. #define KW_I2C_MODE_25KHZ 0x02
  129. #define KW_I2C_MODE_DUMB 0x00
  130. #define KW_I2C_MODE_STANDARD 0x04
  131. #define KW_I2C_MODE_STANDARDSUB 0x08
  132. #define KW_I2C_MODE_COMBINED 0x0C
  133. #define KW_I2C_MODE_MODE_MASK 0x0C
  134. #define KW_I2C_MODE_CHAN_MASK 0xF0
  135. /* Control register */
  136. #define KW_I2C_CTL_AAK 0x01
  137. #define KW_I2C_CTL_XADDR 0x02
  138. #define KW_I2C_CTL_STOP 0x04
  139. #define KW_I2C_CTL_START 0x08
  140. /* Status register */
  141. #define KW_I2C_STAT_BUSY 0x01
  142. #define KW_I2C_STAT_LAST_AAK 0x02
  143. #define KW_I2C_STAT_LAST_RW 0x04
  144. #define KW_I2C_STAT_SDA 0x08
  145. #define KW_I2C_STAT_SCL 0x10
  146. /* IER & ISR registers */
  147. #define KW_I2C_IRQ_DATA 0x01
  148. #define KW_I2C_IRQ_ADDR 0x02
  149. #define KW_I2C_IRQ_STOP 0x04
  150. #define KW_I2C_IRQ_START 0x08
  151. #define KW_I2C_IRQ_MASK 0x0F
  152. /* State machine states */
  153. enum {
  154. state_idle,
  155. state_addr,
  156. state_read,
  157. state_write,
  158. state_stop,
  159. state_dead
  160. };
  161. #define WRONG_STATE(name) do {\
  162. printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s " \
  163. "(isr: %02x)\n", \
  164. name, __kw_state_names[host->state], isr); \
  165. } while(0)
  166. static const char *__kw_state_names[] = {
  167. "state_idle",
  168. "state_addr",
  169. "state_read",
  170. "state_write",
  171. "state_stop",
  172. "state_dead"
  173. };
  174. static inline u8 __kw_read_reg(struct pmac_i2c_host_kw *host, reg_t reg)
  175. {
  176. return readb(host->base + (((unsigned int)reg) << host->bsteps));
  177. }
  178. static inline void __kw_write_reg(struct pmac_i2c_host_kw *host,
  179. reg_t reg, u8 val)
  180. {
  181. writeb(val, host->base + (((unsigned)reg) << host->bsteps));
  182. (void)__kw_read_reg(host, reg_subaddr);
  183. }
  184. #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
  185. #define kw_read_reg(reg) __kw_read_reg(host, reg)
  186. static u8 kw_i2c_wait_interrupt(struct pmac_i2c_host_kw *host)
  187. {
  188. int i, j;
  189. u8 isr;
  190. for (i = 0; i < 1000; i++) {
  191. isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
  192. if (isr != 0)
  193. return isr;
  194. /* This code is used with the timebase frozen, we cannot rely
  195. * on udelay nor schedule when in polled mode !
  196. * For now, just use a bogus loop....
  197. */
  198. if (host->polled) {
  199. for (j = 1; j < 100000; j++)
  200. mb();
  201. } else
  202. msleep(1);
  203. }
  204. return isr;
  205. }
  206. static void kw_i2c_do_stop(struct pmac_i2c_host_kw *host, int result)
  207. {
  208. kw_write_reg(reg_control, KW_I2C_CTL_STOP);
  209. host->state = state_stop;
  210. host->result = result;
  211. }
  212. static void kw_i2c_handle_interrupt(struct pmac_i2c_host_kw *host, u8 isr)
  213. {
  214. u8 ack;
  215. DBG_LOW("kw_handle_interrupt(%s, isr: %x)\n",
  216. __kw_state_names[host->state], isr);
  217. if (host->state == state_idle) {
  218. printk(KERN_WARNING "low_i2c: Keywest got an out of state"
  219. " interrupt, ignoring\n");
  220. kw_write_reg(reg_isr, isr);
  221. return;
  222. }
  223. if (isr == 0) {
  224. printk(KERN_WARNING "low_i2c: Timeout in i2c transfer"
  225. " on keywest !\n");
  226. if (host->state != state_stop) {
  227. kw_i2c_do_stop(host, -EIO);
  228. return;
  229. }
  230. ack = kw_read_reg(reg_status);
  231. if (ack & KW_I2C_STAT_BUSY)
  232. kw_write_reg(reg_status, 0);
  233. host->state = state_idle;
  234. kw_write_reg(reg_ier, 0x00);
  235. if (!host->polled)
  236. complete(&host->complete);
  237. return;
  238. }
  239. if (isr & KW_I2C_IRQ_ADDR) {
  240. ack = kw_read_reg(reg_status);
  241. if (host->state != state_addr) {
  242. WRONG_STATE("KW_I2C_IRQ_ADDR");
  243. kw_i2c_do_stop(host, -EIO);
  244. }
  245. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  246. host->result = -ENXIO;
  247. host->state = state_stop;
  248. DBG_LOW("KW: NAK on address\n");
  249. } else {
  250. if (host->len == 0)
  251. kw_i2c_do_stop(host, 0);
  252. else if (host->rw) {
  253. host->state = state_read;
  254. if (host->len > 1)
  255. kw_write_reg(reg_control,
  256. KW_I2C_CTL_AAK);
  257. } else {
  258. host->state = state_write;
  259. kw_write_reg(reg_data, *(host->data++));
  260. host->len--;
  261. }
  262. }
  263. kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  264. }
  265. if (isr & KW_I2C_IRQ_DATA) {
  266. if (host->state == state_read) {
  267. *(host->data++) = kw_read_reg(reg_data);
  268. host->len--;
  269. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  270. if (host->len == 0)
  271. host->state = state_stop;
  272. else if (host->len == 1)
  273. kw_write_reg(reg_control, 0);
  274. } else if (host->state == state_write) {
  275. ack = kw_read_reg(reg_status);
  276. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  277. DBG_LOW("KW: nack on data write\n");
  278. host->result = -EFBIG;
  279. host->state = state_stop;
  280. } else if (host->len) {
  281. kw_write_reg(reg_data, *(host->data++));
  282. host->len--;
  283. } else
  284. kw_i2c_do_stop(host, 0);
  285. } else {
  286. WRONG_STATE("KW_I2C_IRQ_DATA");
  287. if (host->state != state_stop)
  288. kw_i2c_do_stop(host, -EIO);
  289. }
  290. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  291. }
  292. if (isr & KW_I2C_IRQ_STOP) {
  293. kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
  294. if (host->state != state_stop) {
  295. WRONG_STATE("KW_I2C_IRQ_STOP");
  296. host->result = -EIO;
  297. }
  298. host->state = state_idle;
  299. if (!host->polled)
  300. complete(&host->complete);
  301. }
  302. /* Below should only happen in manual mode which we don't use ... */
  303. if (isr & KW_I2C_IRQ_START)
  304. kw_write_reg(reg_isr, KW_I2C_IRQ_START);
  305. }
  306. /* Interrupt handler */
  307. static irqreturn_t kw_i2c_irq(int irq, void *dev_id)
  308. {
  309. struct pmac_i2c_host_kw *host = dev_id;
  310. unsigned long flags;
  311. spin_lock_irqsave(&host->lock, flags);
  312. del_timer(&host->timeout_timer);
  313. kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
  314. if (host->state != state_idle) {
  315. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  316. add_timer(&host->timeout_timer);
  317. }
  318. spin_unlock_irqrestore(&host->lock, flags);
  319. return IRQ_HANDLED;
  320. }
  321. static void kw_i2c_timeout(unsigned long data)
  322. {
  323. struct pmac_i2c_host_kw *host = (struct pmac_i2c_host_kw *)data;
  324. unsigned long flags;
  325. spin_lock_irqsave(&host->lock, flags);
  326. kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
  327. if (host->state != state_idle) {
  328. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  329. add_timer(&host->timeout_timer);
  330. }
  331. spin_unlock_irqrestore(&host->lock, flags);
  332. }
  333. static int kw_i2c_open(struct pmac_i2c_bus *bus)
  334. {
  335. struct pmac_i2c_host_kw *host = bus->hostdata;
  336. mutex_lock(&host->mutex);
  337. return 0;
  338. }
  339. static void kw_i2c_close(struct pmac_i2c_bus *bus)
  340. {
  341. struct pmac_i2c_host_kw *host = bus->hostdata;
  342. mutex_unlock(&host->mutex);
  343. }
  344. static int kw_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  345. u32 subaddr, u8 *data, int len)
  346. {
  347. struct pmac_i2c_host_kw *host = bus->hostdata;
  348. u8 mode_reg = host->speed;
  349. int use_irq = host->irq != NO_IRQ && !bus->polled;
  350. /* Setup mode & subaddress if any */
  351. switch(bus->mode) {
  352. case pmac_i2c_mode_dumb:
  353. return -EINVAL;
  354. case pmac_i2c_mode_std:
  355. mode_reg |= KW_I2C_MODE_STANDARD;
  356. if (subsize != 0)
  357. return -EINVAL;
  358. break;
  359. case pmac_i2c_mode_stdsub:
  360. mode_reg |= KW_I2C_MODE_STANDARDSUB;
  361. if (subsize != 1)
  362. return -EINVAL;
  363. break;
  364. case pmac_i2c_mode_combined:
  365. mode_reg |= KW_I2C_MODE_COMBINED;
  366. if (subsize != 1)
  367. return -EINVAL;
  368. break;
  369. }
  370. /* Setup channel & clear pending irqs */
  371. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  372. kw_write_reg(reg_mode, mode_reg | (bus->channel << 4));
  373. kw_write_reg(reg_status, 0);
  374. /* Set up address and r/w bit, strip possible stale bus number from
  375. * address top bits
  376. */
  377. kw_write_reg(reg_addr, addrdir & 0xff);
  378. /* Set up the sub address */
  379. if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
  380. || (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
  381. kw_write_reg(reg_subaddr, subaddr);
  382. /* Prepare for async operations */
  383. host->data = data;
  384. host->len = len;
  385. host->state = state_addr;
  386. host->result = 0;
  387. host->rw = (addrdir & 1);
  388. host->polled = bus->polled;
  389. /* Enable interrupt if not using polled mode and interrupt is
  390. * available
  391. */
  392. if (use_irq) {
  393. /* Clear completion */
  394. INIT_COMPLETION(host->complete);
  395. /* Ack stale interrupts */
  396. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  397. /* Arm timeout */
  398. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  399. add_timer(&host->timeout_timer);
  400. /* Enable emission */
  401. kw_write_reg(reg_ier, KW_I2C_IRQ_MASK);
  402. }
  403. /* Start sending address */
  404. kw_write_reg(reg_control, KW_I2C_CTL_XADDR);
  405. /* Wait for completion */
  406. if (use_irq)
  407. wait_for_completion(&host->complete);
  408. else {
  409. while(host->state != state_idle) {
  410. unsigned long flags;
  411. u8 isr = kw_i2c_wait_interrupt(host);
  412. spin_lock_irqsave(&host->lock, flags);
  413. kw_i2c_handle_interrupt(host, isr);
  414. spin_unlock_irqrestore(&host->lock, flags);
  415. }
  416. }
  417. /* Disable emission */
  418. kw_write_reg(reg_ier, 0);
  419. return host->result;
  420. }
  421. static struct pmac_i2c_host_kw *__init kw_i2c_host_init(struct device_node *np)
  422. {
  423. struct pmac_i2c_host_kw *host;
  424. const u32 *psteps, *prate, *addrp;
  425. u32 steps;
  426. host = kzalloc(sizeof(struct pmac_i2c_host_kw), GFP_KERNEL);
  427. if (host == NULL) {
  428. printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
  429. np->full_name);
  430. return NULL;
  431. }
  432. /* Apple is kind enough to provide a valid AAPL,address property
  433. * on all i2c keywest nodes so far ... we would have to fallback
  434. * to macio parsing if that wasn't the case
  435. */
  436. addrp = of_get_property(np, "AAPL,address", NULL);
  437. if (addrp == NULL) {
  438. printk(KERN_ERR "low_i2c: Can't find address for %s\n",
  439. np->full_name);
  440. kfree(host);
  441. return NULL;
  442. }
  443. mutex_init(&host->mutex);
  444. init_completion(&host->complete);
  445. spin_lock_init(&host->lock);
  446. init_timer(&host->timeout_timer);
  447. host->timeout_timer.function = kw_i2c_timeout;
  448. host->timeout_timer.data = (unsigned long)host;
  449. psteps = of_get_property(np, "AAPL,address-step", NULL);
  450. steps = psteps ? (*psteps) : 0x10;
  451. for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
  452. steps >>= 1;
  453. /* Select interface rate */
  454. host->speed = KW_I2C_MODE_25KHZ;
  455. prate = of_get_property(np, "AAPL,i2c-rate", NULL);
  456. if (prate) switch(*prate) {
  457. case 100:
  458. host->speed = KW_I2C_MODE_100KHZ;
  459. break;
  460. case 50:
  461. host->speed = KW_I2C_MODE_50KHZ;
  462. break;
  463. case 25:
  464. host->speed = KW_I2C_MODE_25KHZ;
  465. break;
  466. }
  467. host->irq = irq_of_parse_and_map(np, 0);
  468. if (host->irq == NO_IRQ)
  469. printk(KERN_WARNING
  470. "low_i2c: Failed to map interrupt for %s\n",
  471. np->full_name);
  472. host->base = ioremap((*addrp), 0x1000);
  473. if (host->base == NULL) {
  474. printk(KERN_ERR "low_i2c: Can't map registers for %s\n",
  475. np->full_name);
  476. kfree(host);
  477. return NULL;
  478. }
  479. /* Make sure IRQ is disabled */
  480. kw_write_reg(reg_ier, 0);
  481. /* Request chip interrupt */
  482. if (request_irq(host->irq, kw_i2c_irq, 0, "keywest i2c", host))
  483. host->irq = NO_IRQ;
  484. printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %s\n",
  485. *addrp, host->irq, np->full_name);
  486. return host;
  487. }
  488. static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,
  489. struct device_node *controller,
  490. struct device_node *busnode,
  491. int channel)
  492. {
  493. struct pmac_i2c_bus *bus;
  494. bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);
  495. if (bus == NULL)
  496. return;
  497. bus->controller = of_node_get(controller);
  498. bus->busnode = of_node_get(busnode);
  499. bus->type = pmac_i2c_bus_keywest;
  500. bus->hostdata = host;
  501. bus->channel = channel;
  502. bus->mode = pmac_i2c_mode_std;
  503. bus->open = kw_i2c_open;
  504. bus->close = kw_i2c_close;
  505. bus->xfer = kw_i2c_xfer;
  506. mutex_init(&bus->mutex);
  507. if (controller == busnode)
  508. bus->flags = pmac_i2c_multibus;
  509. list_add(&bus->link, &pmac_i2c_busses);
  510. printk(KERN_INFO " channel %d bus %s\n", channel,
  511. (controller == busnode) ? "<multibus>" : busnode->full_name);
  512. }
  513. static void __init kw_i2c_probe(void)
  514. {
  515. struct device_node *np, *child, *parent;
  516. /* Probe keywest-i2c busses */
  517. for_each_compatible_node(np, "i2c","keywest-i2c") {
  518. struct pmac_i2c_host_kw *host;
  519. int multibus, chans, i;
  520. /* Found one, init a host structure */
  521. host = kw_i2c_host_init(np);
  522. if (host == NULL)
  523. continue;
  524. /* Now check if we have a multibus setup (old style) or if we
  525. * have proper bus nodes. Note that the "new" way (proper bus
  526. * nodes) might cause us to not create some busses that are
  527. * kept hidden in the device-tree. In the future, we might
  528. * want to work around that by creating busses without a node
  529. * but not for now
  530. */
  531. child = of_get_next_child(np, NULL);
  532. multibus = !child || strcmp(child->name, "i2c-bus");
  533. of_node_put(child);
  534. /* For a multibus setup, we get the bus count based on the
  535. * parent type
  536. */
  537. if (multibus) {
  538. parent = of_get_parent(np);
  539. if (parent == NULL)
  540. continue;
  541. chans = parent->name[0] == 'u' ? 2 : 1;
  542. for (i = 0; i < chans; i++)
  543. kw_i2c_add(host, np, np, i);
  544. } else {
  545. for (child = NULL;
  546. (child = of_get_next_child(np, child)) != NULL;) {
  547. const u32 *reg = of_get_property(child,
  548. "reg", NULL);
  549. if (reg == NULL)
  550. continue;
  551. kw_i2c_add(host, np, child, *reg);
  552. }
  553. }
  554. }
  555. }
  556. /*
  557. *
  558. * PMU implementation
  559. *
  560. */
  561. #ifdef CONFIG_ADB_PMU
  562. /*
  563. * i2c command block to the PMU
  564. */
  565. struct pmu_i2c_hdr {
  566. u8 bus;
  567. u8 mode;
  568. u8 bus2;
  569. u8 address;
  570. u8 sub_addr;
  571. u8 comb_addr;
  572. u8 count;
  573. u8 data[];
  574. };
  575. static void pmu_i2c_complete(struct adb_request *req)
  576. {
  577. complete(req->arg);
  578. }
  579. static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  580. u32 subaddr, u8 *data, int len)
  581. {
  582. struct adb_request *req = bus->hostdata;
  583. struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];
  584. struct completion comp;
  585. int read = addrdir & 1;
  586. int retry;
  587. int rc = 0;
  588. /* For now, limit ourselves to 16 bytes transfers */
  589. if (len > 16)
  590. return -EINVAL;
  591. init_completion(&comp);
  592. for (retry = 0; retry < 16; retry++) {
  593. memset(req, 0, sizeof(struct adb_request));
  594. hdr->bus = bus->channel;
  595. hdr->count = len;
  596. switch(bus->mode) {
  597. case pmac_i2c_mode_std:
  598. if (subsize != 0)
  599. return -EINVAL;
  600. hdr->address = addrdir;
  601. hdr->mode = PMU_I2C_MODE_SIMPLE;
  602. break;
  603. case pmac_i2c_mode_stdsub:
  604. case pmac_i2c_mode_combined:
  605. if (subsize != 1)
  606. return -EINVAL;
  607. hdr->address = addrdir & 0xfe;
  608. hdr->comb_addr = addrdir;
  609. hdr->sub_addr = subaddr;
  610. if (bus->mode == pmac_i2c_mode_stdsub)
  611. hdr->mode = PMU_I2C_MODE_STDSUB;
  612. else
  613. hdr->mode = PMU_I2C_MODE_COMBINED;
  614. break;
  615. default:
  616. return -EINVAL;
  617. }
  618. INIT_COMPLETION(comp);
  619. req->data[0] = PMU_I2C_CMD;
  620. req->reply[0] = 0xff;
  621. req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;
  622. req->done = pmu_i2c_complete;
  623. req->arg = &comp;
  624. if (!read && len) {
  625. memcpy(hdr->data, data, len);
  626. req->nbytes += len;
  627. }
  628. rc = pmu_queue_request(req);
  629. if (rc)
  630. return rc;
  631. wait_for_completion(&comp);
  632. if (req->reply[0] == PMU_I2C_STATUS_OK)
  633. break;
  634. msleep(15);
  635. }
  636. if (req->reply[0] != PMU_I2C_STATUS_OK)
  637. return -EIO;
  638. for (retry = 0; retry < 16; retry++) {
  639. memset(req, 0, sizeof(struct adb_request));
  640. /* I know that looks like a lot, slow as hell, but darwin
  641. * does it so let's be on the safe side for now
  642. */
  643. msleep(15);
  644. hdr->bus = PMU_I2C_BUS_STATUS;
  645. INIT_COMPLETION(comp);
  646. req->data[0] = PMU_I2C_CMD;
  647. req->reply[0] = 0xff;
  648. req->nbytes = 2;
  649. req->done = pmu_i2c_complete;
  650. req->arg = &comp;
  651. rc = pmu_queue_request(req);
  652. if (rc)
  653. return rc;
  654. wait_for_completion(&comp);
  655. if (req->reply[0] == PMU_I2C_STATUS_OK && !read)
  656. return 0;
  657. if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {
  658. int rlen = req->reply_len - 1;
  659. if (rlen != len) {
  660. printk(KERN_WARNING "low_i2c: PMU returned %d"
  661. " bytes, expected %d !\n", rlen, len);
  662. return -EIO;
  663. }
  664. if (len)
  665. memcpy(data, &req->reply[1], len);
  666. return 0;
  667. }
  668. }
  669. return -EIO;
  670. }
  671. static void __init pmu_i2c_probe(void)
  672. {
  673. struct pmac_i2c_bus *bus;
  674. struct device_node *busnode;
  675. int channel, sz;
  676. if (!pmu_present())
  677. return;
  678. /* There might or might not be a "pmu-i2c" node, we use that
  679. * or via-pmu itself, whatever we find. I haven't seen a machine
  680. * with separate bus nodes, so we assume a multibus setup
  681. */
  682. busnode = of_find_node_by_name(NULL, "pmu-i2c");
  683. if (busnode == NULL)
  684. busnode = of_find_node_by_name(NULL, "via-pmu");
  685. if (busnode == NULL)
  686. return;
  687. printk(KERN_INFO "PMU i2c %s\n", busnode->full_name);
  688. /*
  689. * We add bus 1 and 2 only for now, bus 0 is "special"
  690. */
  691. for (channel = 1; channel <= 2; channel++) {
  692. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);
  693. bus = kzalloc(sz, GFP_KERNEL);
  694. if (bus == NULL)
  695. return;
  696. bus->controller = busnode;
  697. bus->busnode = busnode;
  698. bus->type = pmac_i2c_bus_pmu;
  699. bus->channel = channel;
  700. bus->mode = pmac_i2c_mode_std;
  701. bus->hostdata = bus + 1;
  702. bus->xfer = pmu_i2c_xfer;
  703. mutex_init(&bus->mutex);
  704. bus->flags = pmac_i2c_multibus;
  705. list_add(&bus->link, &pmac_i2c_busses);
  706. printk(KERN_INFO " channel %d bus <multibus>\n", channel);
  707. }
  708. }
  709. #endif /* CONFIG_ADB_PMU */
  710. /*
  711. *
  712. * SMU implementation
  713. *
  714. */
  715. #ifdef CONFIG_PMAC_SMU
  716. static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)
  717. {
  718. complete(misc);
  719. }
  720. static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  721. u32 subaddr, u8 *data, int len)
  722. {
  723. struct smu_i2c_cmd *cmd = bus->hostdata;
  724. struct completion comp;
  725. int read = addrdir & 1;
  726. int rc = 0;
  727. if ((read && len > SMU_I2C_READ_MAX) ||
  728. ((!read) && len > SMU_I2C_WRITE_MAX))
  729. return -EINVAL;
  730. memset(cmd, 0, sizeof(struct smu_i2c_cmd));
  731. cmd->info.bus = bus->channel;
  732. cmd->info.devaddr = addrdir;
  733. cmd->info.datalen = len;
  734. switch(bus->mode) {
  735. case pmac_i2c_mode_std:
  736. if (subsize != 0)
  737. return -EINVAL;
  738. cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;
  739. break;
  740. case pmac_i2c_mode_stdsub:
  741. case pmac_i2c_mode_combined:
  742. if (subsize > 3 || subsize < 1)
  743. return -EINVAL;
  744. cmd->info.sublen = subsize;
  745. /* that's big-endian only but heh ! */
  746. memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),
  747. subsize);
  748. if (bus->mode == pmac_i2c_mode_stdsub)
  749. cmd->info.type = SMU_I2C_TRANSFER_STDSUB;
  750. else
  751. cmd->info.type = SMU_I2C_TRANSFER_COMBINED;
  752. break;
  753. default:
  754. return -EINVAL;
  755. }
  756. if (!read && len)
  757. memcpy(cmd->info.data, data, len);
  758. init_completion(&comp);
  759. cmd->done = smu_i2c_complete;
  760. cmd->misc = &comp;
  761. rc = smu_queue_i2c(cmd);
  762. if (rc < 0)
  763. return rc;
  764. wait_for_completion(&comp);
  765. rc = cmd->status;
  766. if (read && len)
  767. memcpy(data, cmd->info.data, len);
  768. return rc < 0 ? rc : 0;
  769. }
  770. static void __init smu_i2c_probe(void)
  771. {
  772. struct device_node *controller, *busnode;
  773. struct pmac_i2c_bus *bus;
  774. const u32 *reg;
  775. int sz;
  776. if (!smu_present())
  777. return;
  778. controller = of_find_node_by_name(NULL, "smu-i2c-control");
  779. if (controller == NULL)
  780. controller = of_find_node_by_name(NULL, "smu");
  781. if (controller == NULL)
  782. return;
  783. printk(KERN_INFO "SMU i2c %s\n", controller->full_name);
  784. /* Look for childs, note that they might not be of the right
  785. * type as older device trees mix i2c busses and other thigns
  786. * at the same level
  787. */
  788. for (busnode = NULL;
  789. (busnode = of_get_next_child(controller, busnode)) != NULL;) {
  790. if (strcmp(busnode->type, "i2c") &&
  791. strcmp(busnode->type, "i2c-bus"))
  792. continue;
  793. reg = of_get_property(busnode, "reg", NULL);
  794. if (reg == NULL)
  795. continue;
  796. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);
  797. bus = kzalloc(sz, GFP_KERNEL);
  798. if (bus == NULL)
  799. return;
  800. bus->controller = controller;
  801. bus->busnode = of_node_get(busnode);
  802. bus->type = pmac_i2c_bus_smu;
  803. bus->channel = *reg;
  804. bus->mode = pmac_i2c_mode_std;
  805. bus->hostdata = bus + 1;
  806. bus->xfer = smu_i2c_xfer;
  807. mutex_init(&bus->mutex);
  808. bus->flags = 0;
  809. list_add(&bus->link, &pmac_i2c_busses);
  810. printk(KERN_INFO " channel %x bus %s\n",
  811. bus->channel, busnode->full_name);
  812. }
  813. }
  814. #endif /* CONFIG_PMAC_SMU */
  815. /*
  816. *
  817. * Core code
  818. *
  819. */
  820. struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)
  821. {
  822. struct device_node *p = of_node_get(node);
  823. struct device_node *prev = NULL;
  824. struct pmac_i2c_bus *bus;
  825. while(p) {
  826. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  827. if (p == bus->busnode) {
  828. if (prev && bus->flags & pmac_i2c_multibus) {
  829. const u32 *reg;
  830. reg = of_get_property(prev, "reg",
  831. NULL);
  832. if (!reg)
  833. continue;
  834. if (((*reg) >> 8) != bus->channel)
  835. continue;
  836. }
  837. of_node_put(p);
  838. of_node_put(prev);
  839. return bus;
  840. }
  841. }
  842. of_node_put(prev);
  843. prev = p;
  844. p = of_get_parent(p);
  845. }
  846. return NULL;
  847. }
  848. EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);
  849. u8 pmac_i2c_get_dev_addr(struct device_node *device)
  850. {
  851. const u32 *reg = of_get_property(device, "reg", NULL);
  852. if (reg == NULL)
  853. return 0;
  854. return (*reg) & 0xff;
  855. }
  856. EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);
  857. struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)
  858. {
  859. return bus->controller;
  860. }
  861. EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);
  862. struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)
  863. {
  864. return bus->busnode;
  865. }
  866. EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);
  867. int pmac_i2c_get_type(struct pmac_i2c_bus *bus)
  868. {
  869. return bus->type;
  870. }
  871. EXPORT_SYMBOL_GPL(pmac_i2c_get_type);
  872. int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)
  873. {
  874. return bus->flags;
  875. }
  876. EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);
  877. int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)
  878. {
  879. return bus->channel;
  880. }
  881. EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);
  882. void pmac_i2c_attach_adapter(struct pmac_i2c_bus *bus,
  883. struct i2c_adapter *adapter)
  884. {
  885. WARN_ON(bus->adapter != NULL);
  886. bus->adapter = adapter;
  887. }
  888. EXPORT_SYMBOL_GPL(pmac_i2c_attach_adapter);
  889. void pmac_i2c_detach_adapter(struct pmac_i2c_bus *bus,
  890. struct i2c_adapter *adapter)
  891. {
  892. WARN_ON(bus->adapter != adapter);
  893. bus->adapter = NULL;
  894. }
  895. EXPORT_SYMBOL_GPL(pmac_i2c_detach_adapter);
  896. struct i2c_adapter *pmac_i2c_get_adapter(struct pmac_i2c_bus *bus)
  897. {
  898. return bus->adapter;
  899. }
  900. EXPORT_SYMBOL_GPL(pmac_i2c_get_adapter);
  901. struct pmac_i2c_bus *pmac_i2c_adapter_to_bus(struct i2c_adapter *adapter)
  902. {
  903. struct pmac_i2c_bus *bus;
  904. list_for_each_entry(bus, &pmac_i2c_busses, link)
  905. if (bus->adapter == adapter)
  906. return bus;
  907. return NULL;
  908. }
  909. EXPORT_SYMBOL_GPL(pmac_i2c_adapter_to_bus);
  910. int pmac_i2c_match_adapter(struct device_node *dev, struct i2c_adapter *adapter)
  911. {
  912. struct pmac_i2c_bus *bus = pmac_i2c_find_bus(dev);
  913. if (bus == NULL)
  914. return 0;
  915. return (bus->adapter == adapter);
  916. }
  917. EXPORT_SYMBOL_GPL(pmac_i2c_match_adapter);
  918. int pmac_low_i2c_lock(struct device_node *np)
  919. {
  920. struct pmac_i2c_bus *bus, *found = NULL;
  921. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  922. if (np == bus->controller) {
  923. found = bus;
  924. break;
  925. }
  926. }
  927. if (!found)
  928. return -ENODEV;
  929. return pmac_i2c_open(bus, 0);
  930. }
  931. EXPORT_SYMBOL_GPL(pmac_low_i2c_lock);
  932. int pmac_low_i2c_unlock(struct device_node *np)
  933. {
  934. struct pmac_i2c_bus *bus, *found = NULL;
  935. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  936. if (np == bus->controller) {
  937. found = bus;
  938. break;
  939. }
  940. }
  941. if (!found)
  942. return -ENODEV;
  943. pmac_i2c_close(bus);
  944. return 0;
  945. }
  946. EXPORT_SYMBOL_GPL(pmac_low_i2c_unlock);
  947. int pmac_i2c_open(struct pmac_i2c_bus *bus, int polled)
  948. {
  949. int rc;
  950. mutex_lock(&bus->mutex);
  951. bus->polled = polled || pmac_i2c_force_poll;
  952. bus->opened = 1;
  953. bus->mode = pmac_i2c_mode_std;
  954. if (bus->open && (rc = bus->open(bus)) != 0) {
  955. bus->opened = 0;
  956. mutex_unlock(&bus->mutex);
  957. return rc;
  958. }
  959. return 0;
  960. }
  961. EXPORT_SYMBOL_GPL(pmac_i2c_open);
  962. void pmac_i2c_close(struct pmac_i2c_bus *bus)
  963. {
  964. WARN_ON(!bus->opened);
  965. if (bus->close)
  966. bus->close(bus);
  967. bus->opened = 0;
  968. mutex_unlock(&bus->mutex);
  969. }
  970. EXPORT_SYMBOL_GPL(pmac_i2c_close);
  971. int pmac_i2c_setmode(struct pmac_i2c_bus *bus, int mode)
  972. {
  973. WARN_ON(!bus->opened);
  974. /* Report me if you see the error below as there might be a new
  975. * "combined4" mode that I need to implement for the SMU bus
  976. */
  977. if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {
  978. printk(KERN_ERR "low_i2c: Invalid mode %d requested on"
  979. " bus %s !\n", mode, bus->busnode->full_name);
  980. return -EINVAL;
  981. }
  982. bus->mode = mode;
  983. return 0;
  984. }
  985. EXPORT_SYMBOL_GPL(pmac_i2c_setmode);
  986. int pmac_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  987. u32 subaddr, u8 *data, int len)
  988. {
  989. int rc;
  990. WARN_ON(!bus->opened);
  991. DBG("xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"
  992. " %d bytes, bus %s\n", bus->channel, addrdir, bus->mode, subsize,
  993. subaddr, len, bus->busnode->full_name);
  994. rc = bus->xfer(bus, addrdir, subsize, subaddr, data, len);
  995. #ifdef DEBUG
  996. if (rc)
  997. DBG("xfer error %d\n", rc);
  998. #endif
  999. return rc;
  1000. }
  1001. EXPORT_SYMBOL_GPL(pmac_i2c_xfer);
  1002. /* some quirks for platform function decoding */
  1003. enum {
  1004. pmac_i2c_quirk_invmask = 0x00000001u,
  1005. pmac_i2c_quirk_skip = 0x00000002u,
  1006. };
  1007. static void pmac_i2c_devscan(void (*callback)(struct device_node *dev,
  1008. int quirks))
  1009. {
  1010. struct pmac_i2c_bus *bus;
  1011. struct device_node *np;
  1012. static struct whitelist_ent {
  1013. char *name;
  1014. char *compatible;
  1015. int quirks;
  1016. } whitelist[] = {
  1017. /* XXX Study device-tree's & apple drivers are get the quirks
  1018. * right !
  1019. */
  1020. /* Workaround: It seems that running the clockspreading
  1021. * properties on the eMac will cause lockups during boot.
  1022. * The machine seems to work fine without that. So for now,
  1023. * let's make sure i2c-hwclock doesn't match about "imic"
  1024. * clocks and we'll figure out if we really need to do
  1025. * something special about those later.
  1026. */
  1027. { "i2c-hwclock", "imic5002", pmac_i2c_quirk_skip },
  1028. { "i2c-hwclock", "imic5003", pmac_i2c_quirk_skip },
  1029. { "i2c-hwclock", NULL, pmac_i2c_quirk_invmask },
  1030. { "i2c-cpu-voltage", NULL, 0},
  1031. { "temp-monitor", NULL, 0 },
  1032. { "supply-monitor", NULL, 0 },
  1033. { NULL, NULL, 0 },
  1034. };
  1035. /* Only some devices need to have platform functions instanciated
  1036. * here. For now, we have a table. Others, like 9554 i2c GPIOs used
  1037. * on Xserve, if we ever do a driver for them, will use their own
  1038. * platform function instance
  1039. */
  1040. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  1041. for (np = NULL;
  1042. (np = of_get_next_child(bus->busnode, np)) != NULL;) {
  1043. struct whitelist_ent *p;
  1044. /* If multibus, check if device is on that bus */
  1045. if (bus->flags & pmac_i2c_multibus)
  1046. if (bus != pmac_i2c_find_bus(np))
  1047. continue;
  1048. for (p = whitelist; p->name != NULL; p++) {
  1049. if (strcmp(np->name, p->name))
  1050. continue;
  1051. if (p->compatible &&
  1052. !of_device_is_compatible(np, p->compatible))
  1053. continue;
  1054. if (p->quirks & pmac_i2c_quirk_skip)
  1055. break;
  1056. callback(np, p->quirks);
  1057. break;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. #define MAX_I2C_DATA 64
  1063. struct pmac_i2c_pf_inst
  1064. {
  1065. struct pmac_i2c_bus *bus;
  1066. u8 addr;
  1067. u8 buffer[MAX_I2C_DATA];
  1068. u8 scratch[MAX_I2C_DATA];
  1069. int bytes;
  1070. int quirks;
  1071. };
  1072. static void* pmac_i2c_do_begin(struct pmf_function *func, struct pmf_args *args)
  1073. {
  1074. struct pmac_i2c_pf_inst *inst;
  1075. struct pmac_i2c_bus *bus;
  1076. bus = pmac_i2c_find_bus(func->node);
  1077. if (bus == NULL) {
  1078. printk(KERN_ERR "low_i2c: Can't find bus for %s (pfunc)\n",
  1079. func->node->full_name);
  1080. return NULL;
  1081. }
  1082. if (pmac_i2c_open(bus, 0)) {
  1083. printk(KERN_ERR "low_i2c: Can't open i2c bus for %s (pfunc)\n",
  1084. func->node->full_name);
  1085. return NULL;
  1086. }
  1087. /* XXX might need GFP_ATOMIC when called during the suspend process,
  1088. * but then, there are already lots of issues with suspending when
  1089. * near OOM that need to be resolved, the allocator itself should
  1090. * probably make GFP_NOIO implicit during suspend
  1091. */
  1092. inst = kzalloc(sizeof(struct pmac_i2c_pf_inst), GFP_KERNEL);
  1093. if (inst == NULL) {
  1094. pmac_i2c_close(bus);
  1095. return NULL;
  1096. }
  1097. inst->bus = bus;
  1098. inst->addr = pmac_i2c_get_dev_addr(func->node);
  1099. inst->quirks = (int)(long)func->driver_data;
  1100. return inst;
  1101. }
  1102. static void pmac_i2c_do_end(struct pmf_function *func, void *instdata)
  1103. {
  1104. struct pmac_i2c_pf_inst *inst = instdata;
  1105. if (inst == NULL)
  1106. return;
  1107. pmac_i2c_close(inst->bus);
  1108. if (inst)
  1109. kfree(inst);
  1110. }
  1111. static int pmac_i2c_do_read(PMF_STD_ARGS, u32 len)
  1112. {
  1113. struct pmac_i2c_pf_inst *inst = instdata;
  1114. inst->bytes = len;
  1115. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 0, 0,
  1116. inst->buffer, len);
  1117. }
  1118. static int pmac_i2c_do_write(PMF_STD_ARGS, u32 len, const u8 *data)
  1119. {
  1120. struct pmac_i2c_pf_inst *inst = instdata;
  1121. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1122. (u8 *)data, len);
  1123. }
  1124. /* This function is used to do the masking & OR'ing for the "rmw" type
  1125. * callbacks. Ze should apply the mask and OR in the values in the
  1126. * buffer before writing back. The problem is that it seems that
  1127. * various darwin drivers implement the mask/or differently, thus
  1128. * we need to check the quirks first
  1129. */
  1130. static void pmac_i2c_do_apply_rmw(struct pmac_i2c_pf_inst *inst,
  1131. u32 len, const u8 *mask, const u8 *val)
  1132. {
  1133. int i;
  1134. if (inst->quirks & pmac_i2c_quirk_invmask) {
  1135. for (i = 0; i < len; i ++)
  1136. inst->scratch[i] = (inst->buffer[i] & mask[i]) | val[i];
  1137. } else {
  1138. for (i = 0; i < len; i ++)
  1139. inst->scratch[i] = (inst->buffer[i] & ~mask[i])
  1140. | (val[i] & mask[i]);
  1141. }
  1142. }
  1143. static int pmac_i2c_do_rmw(PMF_STD_ARGS, u32 masklen, u32 valuelen,
  1144. u32 totallen, const u8 *maskdata,
  1145. const u8 *valuedata)
  1146. {
  1147. struct pmac_i2c_pf_inst *inst = instdata;
  1148. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1149. totallen > inst->bytes || valuelen > masklen)
  1150. return -EINVAL;
  1151. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1152. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1153. inst->scratch, totallen);
  1154. }
  1155. static int pmac_i2c_do_read_sub(PMF_STD_ARGS, u8 subaddr, u32 len)
  1156. {
  1157. struct pmac_i2c_pf_inst *inst = instdata;
  1158. inst->bytes = len;
  1159. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 1, subaddr,
  1160. inst->buffer, len);
  1161. }
  1162. static int pmac_i2c_do_write_sub(PMF_STD_ARGS, u8 subaddr, u32 len,
  1163. const u8 *data)
  1164. {
  1165. struct pmac_i2c_pf_inst *inst = instdata;
  1166. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1167. subaddr, (u8 *)data, len);
  1168. }
  1169. static int pmac_i2c_do_set_mode(PMF_STD_ARGS, int mode)
  1170. {
  1171. struct pmac_i2c_pf_inst *inst = instdata;
  1172. return pmac_i2c_setmode(inst->bus, mode);
  1173. }
  1174. static int pmac_i2c_do_rmw_sub(PMF_STD_ARGS, u8 subaddr, u32 masklen,
  1175. u32 valuelen, u32 totallen, const u8 *maskdata,
  1176. const u8 *valuedata)
  1177. {
  1178. struct pmac_i2c_pf_inst *inst = instdata;
  1179. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1180. totallen > inst->bytes || valuelen > masklen)
  1181. return -EINVAL;
  1182. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1183. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1184. subaddr, inst->scratch, totallen);
  1185. }
  1186. static int pmac_i2c_do_mask_and_comp(PMF_STD_ARGS, u32 len,
  1187. const u8 *maskdata,
  1188. const u8 *valuedata)
  1189. {
  1190. struct pmac_i2c_pf_inst *inst = instdata;
  1191. int i, match;
  1192. /* Get return value pointer, it's assumed to be a u32 */
  1193. if (!args || !args->count || !args->u[0].p)
  1194. return -EINVAL;
  1195. /* Check buffer */
  1196. if (len > inst->bytes)
  1197. return -EINVAL;
  1198. for (i = 0, match = 1; match && i < len; i ++)
  1199. if ((inst->buffer[i] & maskdata[i]) != valuedata[i])
  1200. match = 0;
  1201. *args->u[0].p = match;
  1202. return 0;
  1203. }
  1204. static int pmac_i2c_do_delay(PMF_STD_ARGS, u32 duration)
  1205. {
  1206. msleep((duration + 999) / 1000);
  1207. return 0;
  1208. }
  1209. static struct pmf_handlers pmac_i2c_pfunc_handlers = {
  1210. .begin = pmac_i2c_do_begin,
  1211. .end = pmac_i2c_do_end,
  1212. .read_i2c = pmac_i2c_do_read,
  1213. .write_i2c = pmac_i2c_do_write,
  1214. .rmw_i2c = pmac_i2c_do_rmw,
  1215. .read_i2c_sub = pmac_i2c_do_read_sub,
  1216. .write_i2c_sub = pmac_i2c_do_write_sub,
  1217. .rmw_i2c_sub = pmac_i2c_do_rmw_sub,
  1218. .set_i2c_mode = pmac_i2c_do_set_mode,
  1219. .mask_and_compare = pmac_i2c_do_mask_and_comp,
  1220. .delay = pmac_i2c_do_delay,
  1221. };
  1222. static void __init pmac_i2c_dev_create(struct device_node *np, int quirks)
  1223. {
  1224. DBG("dev_create(%s)\n", np->full_name);
  1225. pmf_register_driver(np, &pmac_i2c_pfunc_handlers,
  1226. (void *)(long)quirks);
  1227. }
  1228. static void __init pmac_i2c_dev_init(struct device_node *np, int quirks)
  1229. {
  1230. DBG("dev_create(%s)\n", np->full_name);
  1231. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
  1232. }
  1233. static void pmac_i2c_dev_suspend(struct device_node *np, int quirks)
  1234. {
  1235. DBG("dev_suspend(%s)\n", np->full_name);
  1236. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_SLEEP, NULL);
  1237. }
  1238. static void pmac_i2c_dev_resume(struct device_node *np, int quirks)
  1239. {
  1240. DBG("dev_resume(%s)\n", np->full_name);
  1241. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_WAKE, NULL);
  1242. }
  1243. void pmac_pfunc_i2c_suspend(void)
  1244. {
  1245. pmac_i2c_devscan(pmac_i2c_dev_suspend);
  1246. }
  1247. void pmac_pfunc_i2c_resume(void)
  1248. {
  1249. pmac_i2c_devscan(pmac_i2c_dev_resume);
  1250. }
  1251. /*
  1252. * Initialize us: probe all i2c busses on the machine, instantiate
  1253. * busses and platform functions as needed.
  1254. */
  1255. /* This is non-static as it might be called early by smp code */
  1256. int __init pmac_i2c_init(void)
  1257. {
  1258. static int i2c_inited;
  1259. if (i2c_inited)
  1260. return 0;
  1261. i2c_inited = 1;
  1262. /* Probe keywest-i2c busses */
  1263. kw_i2c_probe();
  1264. #ifdef CONFIG_ADB_PMU
  1265. /* Probe PMU i2c busses */
  1266. pmu_i2c_probe();
  1267. #endif
  1268. #ifdef CONFIG_PMAC_SMU
  1269. /* Probe SMU i2c busses */
  1270. smu_i2c_probe();
  1271. #endif
  1272. /* Now add plaform functions for some known devices */
  1273. pmac_i2c_devscan(pmac_i2c_dev_create);
  1274. return 0;
  1275. }
  1276. machine_arch_initcall(powermac, pmac_i2c_init);
  1277. /* Since pmac_i2c_init can be called too early for the platform device
  1278. * registration, we need to do it at a later time. In our case, subsys
  1279. * happens to fit well, though I agree it's a bit of a hack...
  1280. */
  1281. static int __init pmac_i2c_create_platform_devices(void)
  1282. {
  1283. struct pmac_i2c_bus *bus;
  1284. int i = 0;
  1285. /* In the case where we are initialized from smp_init(), we must
  1286. * not use the timer (and thus the irq). It's safe from now on
  1287. * though
  1288. */
  1289. pmac_i2c_force_poll = 0;
  1290. /* Create platform devices */
  1291. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  1292. bus->platform_dev =
  1293. platform_device_alloc("i2c-powermac", i++);
  1294. if (bus->platform_dev == NULL)
  1295. return -ENOMEM;
  1296. bus->platform_dev->dev.platform_data = bus;
  1297. platform_device_add(bus->platform_dev);
  1298. }
  1299. /* Now call platform "init" functions */
  1300. pmac_i2c_devscan(pmac_i2c_dev_init);
  1301. return 0;
  1302. }
  1303. machine_subsys_initcall(powermac, pmac_i2c_create_platform_devices);