low_i2c.c 36 KB

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