low_i2c.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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 chipsets 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/export.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. /*
  329. * If the timer is pending, that means we raced with the
  330. * irq, in which case we just return
  331. */
  332. if (timer_pending(&host->timeout_timer))
  333. goto skip;
  334. kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
  335. if (host->state != state_idle) {
  336. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  337. add_timer(&host->timeout_timer);
  338. }
  339. skip:
  340. spin_unlock_irqrestore(&host->lock, flags);
  341. }
  342. static int kw_i2c_open(struct pmac_i2c_bus *bus)
  343. {
  344. struct pmac_i2c_host_kw *host = bus->hostdata;
  345. mutex_lock(&host->mutex);
  346. return 0;
  347. }
  348. static void kw_i2c_close(struct pmac_i2c_bus *bus)
  349. {
  350. struct pmac_i2c_host_kw *host = bus->hostdata;
  351. mutex_unlock(&host->mutex);
  352. }
  353. static int kw_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  354. u32 subaddr, u8 *data, int len)
  355. {
  356. struct pmac_i2c_host_kw *host = bus->hostdata;
  357. u8 mode_reg = host->speed;
  358. int use_irq = host->irq && !bus->polled;
  359. /* Setup mode & subaddress if any */
  360. switch(bus->mode) {
  361. case pmac_i2c_mode_dumb:
  362. return -EINVAL;
  363. case pmac_i2c_mode_std:
  364. mode_reg |= KW_I2C_MODE_STANDARD;
  365. if (subsize != 0)
  366. return -EINVAL;
  367. break;
  368. case pmac_i2c_mode_stdsub:
  369. mode_reg |= KW_I2C_MODE_STANDARDSUB;
  370. if (subsize != 1)
  371. return -EINVAL;
  372. break;
  373. case pmac_i2c_mode_combined:
  374. mode_reg |= KW_I2C_MODE_COMBINED;
  375. if (subsize != 1)
  376. return -EINVAL;
  377. break;
  378. }
  379. /* Setup channel & clear pending irqs */
  380. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  381. kw_write_reg(reg_mode, mode_reg | (bus->channel << 4));
  382. kw_write_reg(reg_status, 0);
  383. /* Set up address and r/w bit, strip possible stale bus number from
  384. * address top bits
  385. */
  386. kw_write_reg(reg_addr, addrdir & 0xff);
  387. /* Set up the sub address */
  388. if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
  389. || (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
  390. kw_write_reg(reg_subaddr, subaddr);
  391. /* Prepare for async operations */
  392. host->data = data;
  393. host->len = len;
  394. host->state = state_addr;
  395. host->result = 0;
  396. host->rw = (addrdir & 1);
  397. host->polled = bus->polled;
  398. /* Enable interrupt if not using polled mode and interrupt is
  399. * available
  400. */
  401. if (use_irq) {
  402. /* Clear completion */
  403. reinit_completion(&host->complete);
  404. /* Ack stale interrupts */
  405. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  406. /* Arm timeout */
  407. host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
  408. add_timer(&host->timeout_timer);
  409. /* Enable emission */
  410. kw_write_reg(reg_ier, KW_I2C_IRQ_MASK);
  411. }
  412. /* Start sending address */
  413. kw_write_reg(reg_control, KW_I2C_CTL_XADDR);
  414. /* Wait for completion */
  415. if (use_irq)
  416. wait_for_completion(&host->complete);
  417. else {
  418. while(host->state != state_idle) {
  419. unsigned long flags;
  420. u8 isr = kw_i2c_wait_interrupt(host);
  421. spin_lock_irqsave(&host->lock, flags);
  422. kw_i2c_handle_interrupt(host, isr);
  423. spin_unlock_irqrestore(&host->lock, flags);
  424. }
  425. }
  426. /* Disable emission */
  427. kw_write_reg(reg_ier, 0);
  428. return host->result;
  429. }
  430. static struct pmac_i2c_host_kw *__init kw_i2c_host_init(struct device_node *np)
  431. {
  432. struct pmac_i2c_host_kw *host;
  433. const u32 *psteps, *prate, *addrp;
  434. u32 steps;
  435. host = kzalloc(sizeof(struct pmac_i2c_host_kw), GFP_KERNEL);
  436. if (host == NULL) {
  437. printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
  438. np->full_name);
  439. return NULL;
  440. }
  441. /* Apple is kind enough to provide a valid AAPL,address property
  442. * on all i2c keywest nodes so far ... we would have to fallback
  443. * to macio parsing if that wasn't the case
  444. */
  445. addrp = of_get_property(np, "AAPL,address", NULL);
  446. if (addrp == NULL) {
  447. printk(KERN_ERR "low_i2c: Can't find address for %s\n",
  448. np->full_name);
  449. kfree(host);
  450. return NULL;
  451. }
  452. mutex_init(&host->mutex);
  453. init_completion(&host->complete);
  454. spin_lock_init(&host->lock);
  455. init_timer(&host->timeout_timer);
  456. host->timeout_timer.function = kw_i2c_timeout;
  457. host->timeout_timer.data = (unsigned long)host;
  458. psteps = of_get_property(np, "AAPL,address-step", NULL);
  459. steps = psteps ? (*psteps) : 0x10;
  460. for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
  461. steps >>= 1;
  462. /* Select interface rate */
  463. host->speed = KW_I2C_MODE_25KHZ;
  464. prate = of_get_property(np, "AAPL,i2c-rate", NULL);
  465. if (prate) switch(*prate) {
  466. case 100:
  467. host->speed = KW_I2C_MODE_100KHZ;
  468. break;
  469. case 50:
  470. host->speed = KW_I2C_MODE_50KHZ;
  471. break;
  472. case 25:
  473. host->speed = KW_I2C_MODE_25KHZ;
  474. break;
  475. }
  476. host->irq = irq_of_parse_and_map(np, 0);
  477. if (!host->irq)
  478. printk(KERN_WARNING
  479. "low_i2c: Failed to map interrupt for %s\n",
  480. np->full_name);
  481. host->base = ioremap((*addrp), 0x1000);
  482. if (host->base == NULL) {
  483. printk(KERN_ERR "low_i2c: Can't map registers for %s\n",
  484. np->full_name);
  485. kfree(host);
  486. return NULL;
  487. }
  488. /* Make sure IRQ is disabled */
  489. kw_write_reg(reg_ier, 0);
  490. /* Request chip interrupt. We set IRQF_NO_SUSPEND because we don't
  491. * want that interrupt disabled between the 2 passes of driver
  492. * suspend or we'll have issues running the pfuncs
  493. */
  494. if (request_irq(host->irq, kw_i2c_irq, IRQF_NO_SUSPEND,
  495. "keywest i2c", host))
  496. host->irq = 0;
  497. printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %s\n",
  498. *addrp, host->irq, np->full_name);
  499. return host;
  500. }
  501. static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,
  502. struct device_node *controller,
  503. struct device_node *busnode,
  504. int channel)
  505. {
  506. struct pmac_i2c_bus *bus;
  507. bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);
  508. if (bus == NULL)
  509. return;
  510. bus->controller = of_node_get(controller);
  511. bus->busnode = of_node_get(busnode);
  512. bus->type = pmac_i2c_bus_keywest;
  513. bus->hostdata = host;
  514. bus->channel = channel;
  515. bus->mode = pmac_i2c_mode_std;
  516. bus->open = kw_i2c_open;
  517. bus->close = kw_i2c_close;
  518. bus->xfer = kw_i2c_xfer;
  519. mutex_init(&bus->mutex);
  520. if (controller == busnode)
  521. bus->flags = pmac_i2c_multibus;
  522. list_add(&bus->link, &pmac_i2c_busses);
  523. printk(KERN_INFO " channel %d bus %s\n", channel,
  524. (controller == busnode) ? "<multibus>" : busnode->full_name);
  525. }
  526. static void __init kw_i2c_probe(void)
  527. {
  528. struct device_node *np, *child, *parent;
  529. /* Probe keywest-i2c busses */
  530. for_each_compatible_node(np, "i2c","keywest-i2c") {
  531. struct pmac_i2c_host_kw *host;
  532. int multibus;
  533. /* Found one, init a host structure */
  534. host = kw_i2c_host_init(np);
  535. if (host == NULL)
  536. continue;
  537. /* Now check if we have a multibus setup (old style) or if we
  538. * have proper bus nodes. Note that the "new" way (proper bus
  539. * nodes) might cause us to not create some busses that are
  540. * kept hidden in the device-tree. In the future, we might
  541. * want to work around that by creating busses without a node
  542. * but not for now
  543. */
  544. child = of_get_next_child(np, NULL);
  545. multibus = !child || strcmp(child->name, "i2c-bus");
  546. of_node_put(child);
  547. /* For a multibus setup, we get the bus count based on the
  548. * parent type
  549. */
  550. if (multibus) {
  551. int chans, i;
  552. parent = of_get_parent(np);
  553. if (parent == NULL)
  554. continue;
  555. chans = parent->name[0] == 'u' ? 2 : 1;
  556. for (i = 0; i < chans; i++)
  557. kw_i2c_add(host, np, np, i);
  558. } else {
  559. for (child = NULL;
  560. (child = of_get_next_child(np, child)) != NULL;) {
  561. const u32 *reg = of_get_property(child,
  562. "reg", NULL);
  563. if (reg == NULL)
  564. continue;
  565. kw_i2c_add(host, np, child, *reg);
  566. }
  567. }
  568. }
  569. }
  570. /*
  571. *
  572. * PMU implementation
  573. *
  574. */
  575. #ifdef CONFIG_ADB_PMU
  576. /*
  577. * i2c command block to the PMU
  578. */
  579. struct pmu_i2c_hdr {
  580. u8 bus;
  581. u8 mode;
  582. u8 bus2;
  583. u8 address;
  584. u8 sub_addr;
  585. u8 comb_addr;
  586. u8 count;
  587. u8 data[];
  588. };
  589. static void pmu_i2c_complete(struct adb_request *req)
  590. {
  591. complete(req->arg);
  592. }
  593. static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  594. u32 subaddr, u8 *data, int len)
  595. {
  596. struct adb_request *req = bus->hostdata;
  597. struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];
  598. struct completion comp;
  599. int read = addrdir & 1;
  600. int retry;
  601. int rc = 0;
  602. /* For now, limit ourselves to 16 bytes transfers */
  603. if (len > 16)
  604. return -EINVAL;
  605. init_completion(&comp);
  606. for (retry = 0; retry < 16; retry++) {
  607. memset(req, 0, sizeof(struct adb_request));
  608. hdr->bus = bus->channel;
  609. hdr->count = len;
  610. switch(bus->mode) {
  611. case pmac_i2c_mode_std:
  612. if (subsize != 0)
  613. return -EINVAL;
  614. hdr->address = addrdir;
  615. hdr->mode = PMU_I2C_MODE_SIMPLE;
  616. break;
  617. case pmac_i2c_mode_stdsub:
  618. case pmac_i2c_mode_combined:
  619. if (subsize != 1)
  620. return -EINVAL;
  621. hdr->address = addrdir & 0xfe;
  622. hdr->comb_addr = addrdir;
  623. hdr->sub_addr = subaddr;
  624. if (bus->mode == pmac_i2c_mode_stdsub)
  625. hdr->mode = PMU_I2C_MODE_STDSUB;
  626. else
  627. hdr->mode = PMU_I2C_MODE_COMBINED;
  628. break;
  629. default:
  630. return -EINVAL;
  631. }
  632. reinit_completion(&comp);
  633. req->data[0] = PMU_I2C_CMD;
  634. req->reply[0] = 0xff;
  635. req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;
  636. req->done = pmu_i2c_complete;
  637. req->arg = &comp;
  638. if (!read && len) {
  639. memcpy(hdr->data, data, len);
  640. req->nbytes += len;
  641. }
  642. rc = pmu_queue_request(req);
  643. if (rc)
  644. return rc;
  645. wait_for_completion(&comp);
  646. if (req->reply[0] == PMU_I2C_STATUS_OK)
  647. break;
  648. msleep(15);
  649. }
  650. if (req->reply[0] != PMU_I2C_STATUS_OK)
  651. return -EIO;
  652. for (retry = 0; retry < 16; retry++) {
  653. memset(req, 0, sizeof(struct adb_request));
  654. /* I know that looks like a lot, slow as hell, but darwin
  655. * does it so let's be on the safe side for now
  656. */
  657. msleep(15);
  658. hdr->bus = PMU_I2C_BUS_STATUS;
  659. reinit_completion(&comp);
  660. req->data[0] = PMU_I2C_CMD;
  661. req->reply[0] = 0xff;
  662. req->nbytes = 2;
  663. req->done = pmu_i2c_complete;
  664. req->arg = &comp;
  665. rc = pmu_queue_request(req);
  666. if (rc)
  667. return rc;
  668. wait_for_completion(&comp);
  669. if (req->reply[0] == PMU_I2C_STATUS_OK && !read)
  670. return 0;
  671. if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {
  672. int rlen = req->reply_len - 1;
  673. if (rlen != len) {
  674. printk(KERN_WARNING "low_i2c: PMU returned %d"
  675. " bytes, expected %d !\n", rlen, len);
  676. return -EIO;
  677. }
  678. if (len)
  679. memcpy(data, &req->reply[1], len);
  680. return 0;
  681. }
  682. }
  683. return -EIO;
  684. }
  685. static void __init pmu_i2c_probe(void)
  686. {
  687. struct pmac_i2c_bus *bus;
  688. struct device_node *busnode;
  689. int channel, sz;
  690. if (!pmu_present())
  691. return;
  692. /* There might or might not be a "pmu-i2c" node, we use that
  693. * or via-pmu itself, whatever we find. I haven't seen a machine
  694. * with separate bus nodes, so we assume a multibus setup
  695. */
  696. busnode = of_find_node_by_name(NULL, "pmu-i2c");
  697. if (busnode == NULL)
  698. busnode = of_find_node_by_name(NULL, "via-pmu");
  699. if (busnode == NULL)
  700. return;
  701. printk(KERN_INFO "PMU i2c %s\n", busnode->full_name);
  702. /*
  703. * We add bus 1 and 2 only for now, bus 0 is "special"
  704. */
  705. for (channel = 1; channel <= 2; channel++) {
  706. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);
  707. bus = kzalloc(sz, GFP_KERNEL);
  708. if (bus == NULL)
  709. return;
  710. bus->controller = busnode;
  711. bus->busnode = busnode;
  712. bus->type = pmac_i2c_bus_pmu;
  713. bus->channel = channel;
  714. bus->mode = pmac_i2c_mode_std;
  715. bus->hostdata = bus + 1;
  716. bus->xfer = pmu_i2c_xfer;
  717. mutex_init(&bus->mutex);
  718. bus->flags = pmac_i2c_multibus;
  719. list_add(&bus->link, &pmac_i2c_busses);
  720. printk(KERN_INFO " channel %d bus <multibus>\n", channel);
  721. }
  722. }
  723. #endif /* CONFIG_ADB_PMU */
  724. /*
  725. *
  726. * SMU implementation
  727. *
  728. */
  729. #ifdef CONFIG_PMAC_SMU
  730. static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)
  731. {
  732. complete(misc);
  733. }
  734. static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
  735. u32 subaddr, u8 *data, int len)
  736. {
  737. struct smu_i2c_cmd *cmd = bus->hostdata;
  738. struct completion comp;
  739. int read = addrdir & 1;
  740. int rc = 0;
  741. if ((read && len > SMU_I2C_READ_MAX) ||
  742. ((!read) && len > SMU_I2C_WRITE_MAX))
  743. return -EINVAL;
  744. memset(cmd, 0, sizeof(struct smu_i2c_cmd));
  745. cmd->info.bus = bus->channel;
  746. cmd->info.devaddr = addrdir;
  747. cmd->info.datalen = len;
  748. switch(bus->mode) {
  749. case pmac_i2c_mode_std:
  750. if (subsize != 0)
  751. return -EINVAL;
  752. cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;
  753. break;
  754. case pmac_i2c_mode_stdsub:
  755. case pmac_i2c_mode_combined:
  756. if (subsize > 3 || subsize < 1)
  757. return -EINVAL;
  758. cmd->info.sublen = subsize;
  759. /* that's big-endian only but heh ! */
  760. memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),
  761. subsize);
  762. if (bus->mode == pmac_i2c_mode_stdsub)
  763. cmd->info.type = SMU_I2C_TRANSFER_STDSUB;
  764. else
  765. cmd->info.type = SMU_I2C_TRANSFER_COMBINED;
  766. break;
  767. default:
  768. return -EINVAL;
  769. }
  770. if (!read && len)
  771. memcpy(cmd->info.data, data, len);
  772. init_completion(&comp);
  773. cmd->done = smu_i2c_complete;
  774. cmd->misc = &comp;
  775. rc = smu_queue_i2c(cmd);
  776. if (rc < 0)
  777. return rc;
  778. wait_for_completion(&comp);
  779. rc = cmd->status;
  780. if (read && len)
  781. memcpy(data, cmd->info.data, len);
  782. return rc < 0 ? rc : 0;
  783. }
  784. static void __init smu_i2c_probe(void)
  785. {
  786. struct device_node *controller, *busnode;
  787. struct pmac_i2c_bus *bus;
  788. const u32 *reg;
  789. int sz;
  790. if (!smu_present())
  791. return;
  792. controller = of_find_node_by_name(NULL, "smu-i2c-control");
  793. if (controller == NULL)
  794. controller = of_find_node_by_name(NULL, "smu");
  795. if (controller == NULL)
  796. return;
  797. printk(KERN_INFO "SMU i2c %s\n", controller->full_name);
  798. /* Look for childs, note that they might not be of the right
  799. * type as older device trees mix i2c busses and other things
  800. * at the same level
  801. */
  802. for (busnode = NULL;
  803. (busnode = of_get_next_child(controller, busnode)) != NULL;) {
  804. if (strcmp(busnode->type, "i2c") &&
  805. strcmp(busnode->type, "i2c-bus"))
  806. continue;
  807. reg = of_get_property(busnode, "reg", NULL);
  808. if (reg == NULL)
  809. continue;
  810. sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);
  811. bus = kzalloc(sz, GFP_KERNEL);
  812. if (bus == NULL)
  813. return;
  814. bus->controller = controller;
  815. bus->busnode = of_node_get(busnode);
  816. bus->type = pmac_i2c_bus_smu;
  817. bus->channel = *reg;
  818. bus->mode = pmac_i2c_mode_std;
  819. bus->hostdata = bus + 1;
  820. bus->xfer = smu_i2c_xfer;
  821. mutex_init(&bus->mutex);
  822. bus->flags = 0;
  823. list_add(&bus->link, &pmac_i2c_busses);
  824. printk(KERN_INFO " channel %x bus %s\n",
  825. bus->channel, busnode->full_name);
  826. }
  827. }
  828. #endif /* CONFIG_PMAC_SMU */
  829. /*
  830. *
  831. * Core code
  832. *
  833. */
  834. struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)
  835. {
  836. struct device_node *p = of_node_get(node);
  837. struct device_node *prev = NULL;
  838. struct pmac_i2c_bus *bus;
  839. while(p) {
  840. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  841. if (p == bus->busnode) {
  842. if (prev && bus->flags & pmac_i2c_multibus) {
  843. const u32 *reg;
  844. reg = of_get_property(prev, "reg",
  845. NULL);
  846. if (!reg)
  847. continue;
  848. if (((*reg) >> 8) != bus->channel)
  849. continue;
  850. }
  851. of_node_put(p);
  852. of_node_put(prev);
  853. return bus;
  854. }
  855. }
  856. of_node_put(prev);
  857. prev = p;
  858. p = of_get_parent(p);
  859. }
  860. return NULL;
  861. }
  862. EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);
  863. u8 pmac_i2c_get_dev_addr(struct device_node *device)
  864. {
  865. const u32 *reg = of_get_property(device, "reg", NULL);
  866. if (reg == NULL)
  867. return 0;
  868. return (*reg) & 0xff;
  869. }
  870. EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);
  871. struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)
  872. {
  873. return bus->controller;
  874. }
  875. EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);
  876. struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)
  877. {
  878. return bus->busnode;
  879. }
  880. EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);
  881. int pmac_i2c_get_type(struct pmac_i2c_bus *bus)
  882. {
  883. return bus->type;
  884. }
  885. EXPORT_SYMBOL_GPL(pmac_i2c_get_type);
  886. int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)
  887. {
  888. return bus->flags;
  889. }
  890. EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);
  891. int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)
  892. {
  893. return bus->channel;
  894. }
  895. EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);
  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. kfree(inst);
  1109. }
  1110. static int pmac_i2c_do_read(PMF_STD_ARGS, u32 len)
  1111. {
  1112. struct pmac_i2c_pf_inst *inst = instdata;
  1113. inst->bytes = len;
  1114. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 0, 0,
  1115. inst->buffer, len);
  1116. }
  1117. static int pmac_i2c_do_write(PMF_STD_ARGS, u32 len, const u8 *data)
  1118. {
  1119. struct pmac_i2c_pf_inst *inst = instdata;
  1120. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1121. (u8 *)data, len);
  1122. }
  1123. /* This function is used to do the masking & OR'ing for the "rmw" type
  1124. * callbacks. Ze should apply the mask and OR in the values in the
  1125. * buffer before writing back. The problem is that it seems that
  1126. * various darwin drivers implement the mask/or differently, thus
  1127. * we need to check the quirks first
  1128. */
  1129. static void pmac_i2c_do_apply_rmw(struct pmac_i2c_pf_inst *inst,
  1130. u32 len, const u8 *mask, const u8 *val)
  1131. {
  1132. int i;
  1133. if (inst->quirks & pmac_i2c_quirk_invmask) {
  1134. for (i = 0; i < len; i ++)
  1135. inst->scratch[i] = (inst->buffer[i] & mask[i]) | val[i];
  1136. } else {
  1137. for (i = 0; i < len; i ++)
  1138. inst->scratch[i] = (inst->buffer[i] & ~mask[i])
  1139. | (val[i] & mask[i]);
  1140. }
  1141. }
  1142. static int pmac_i2c_do_rmw(PMF_STD_ARGS, u32 masklen, u32 valuelen,
  1143. u32 totallen, const u8 *maskdata,
  1144. const u8 *valuedata)
  1145. {
  1146. struct pmac_i2c_pf_inst *inst = instdata;
  1147. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1148. totallen > inst->bytes || valuelen > masklen)
  1149. return -EINVAL;
  1150. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1151. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,
  1152. inst->scratch, totallen);
  1153. }
  1154. static int pmac_i2c_do_read_sub(PMF_STD_ARGS, u8 subaddr, u32 len)
  1155. {
  1156. struct pmac_i2c_pf_inst *inst = instdata;
  1157. inst->bytes = len;
  1158. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 1, subaddr,
  1159. inst->buffer, len);
  1160. }
  1161. static int pmac_i2c_do_write_sub(PMF_STD_ARGS, u8 subaddr, u32 len,
  1162. const u8 *data)
  1163. {
  1164. struct pmac_i2c_pf_inst *inst = instdata;
  1165. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1166. subaddr, (u8 *)data, len);
  1167. }
  1168. static int pmac_i2c_do_set_mode(PMF_STD_ARGS, int mode)
  1169. {
  1170. struct pmac_i2c_pf_inst *inst = instdata;
  1171. return pmac_i2c_setmode(inst->bus, mode);
  1172. }
  1173. static int pmac_i2c_do_rmw_sub(PMF_STD_ARGS, u8 subaddr, u32 masklen,
  1174. u32 valuelen, u32 totallen, const u8 *maskdata,
  1175. const u8 *valuedata)
  1176. {
  1177. struct pmac_i2c_pf_inst *inst = instdata;
  1178. if (masklen > inst->bytes || valuelen > inst->bytes ||
  1179. totallen > inst->bytes || valuelen > masklen)
  1180. return -EINVAL;
  1181. pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
  1182. return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,
  1183. subaddr, inst->scratch, totallen);
  1184. }
  1185. static int pmac_i2c_do_mask_and_comp(PMF_STD_ARGS, u32 len,
  1186. const u8 *maskdata,
  1187. const u8 *valuedata)
  1188. {
  1189. struct pmac_i2c_pf_inst *inst = instdata;
  1190. int i, match;
  1191. /* Get return value pointer, it's assumed to be a u32 */
  1192. if (!args || !args->count || !args->u[0].p)
  1193. return -EINVAL;
  1194. /* Check buffer */
  1195. if (len > inst->bytes)
  1196. return -EINVAL;
  1197. for (i = 0, match = 1; match && i < len; i ++)
  1198. if ((inst->buffer[i] & maskdata[i]) != valuedata[i])
  1199. match = 0;
  1200. *args->u[0].p = match;
  1201. return 0;
  1202. }
  1203. static int pmac_i2c_do_delay(PMF_STD_ARGS, u32 duration)
  1204. {
  1205. msleep((duration + 999) / 1000);
  1206. return 0;
  1207. }
  1208. static struct pmf_handlers pmac_i2c_pfunc_handlers = {
  1209. .begin = pmac_i2c_do_begin,
  1210. .end = pmac_i2c_do_end,
  1211. .read_i2c = pmac_i2c_do_read,
  1212. .write_i2c = pmac_i2c_do_write,
  1213. .rmw_i2c = pmac_i2c_do_rmw,
  1214. .read_i2c_sub = pmac_i2c_do_read_sub,
  1215. .write_i2c_sub = pmac_i2c_do_write_sub,
  1216. .rmw_i2c_sub = pmac_i2c_do_rmw_sub,
  1217. .set_i2c_mode = pmac_i2c_do_set_mode,
  1218. .mask_and_compare = pmac_i2c_do_mask_and_comp,
  1219. .delay = pmac_i2c_do_delay,
  1220. };
  1221. static void __init pmac_i2c_dev_create(struct device_node *np, int quirks)
  1222. {
  1223. DBG("dev_create(%s)\n", np->full_name);
  1224. pmf_register_driver(np, &pmac_i2c_pfunc_handlers,
  1225. (void *)(long)quirks);
  1226. }
  1227. static void __init pmac_i2c_dev_init(struct device_node *np, int quirks)
  1228. {
  1229. DBG("dev_create(%s)\n", np->full_name);
  1230. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
  1231. }
  1232. static void pmac_i2c_dev_suspend(struct device_node *np, int quirks)
  1233. {
  1234. DBG("dev_suspend(%s)\n", np->full_name);
  1235. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_SLEEP, NULL);
  1236. }
  1237. static void pmac_i2c_dev_resume(struct device_node *np, int quirks)
  1238. {
  1239. DBG("dev_resume(%s)\n", np->full_name);
  1240. pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_WAKE, NULL);
  1241. }
  1242. void pmac_pfunc_i2c_suspend(void)
  1243. {
  1244. pmac_i2c_devscan(pmac_i2c_dev_suspend);
  1245. }
  1246. void pmac_pfunc_i2c_resume(void)
  1247. {
  1248. pmac_i2c_devscan(pmac_i2c_dev_resume);
  1249. }
  1250. /*
  1251. * Initialize us: probe all i2c busses on the machine, instantiate
  1252. * busses and platform functions as needed.
  1253. */
  1254. /* This is non-static as it might be called early by smp code */
  1255. int __init pmac_i2c_init(void)
  1256. {
  1257. static int i2c_inited;
  1258. if (i2c_inited)
  1259. return 0;
  1260. i2c_inited = 1;
  1261. /* Probe keywest-i2c busses */
  1262. kw_i2c_probe();
  1263. #ifdef CONFIG_ADB_PMU
  1264. /* Probe PMU i2c busses */
  1265. pmu_i2c_probe();
  1266. #endif
  1267. #ifdef CONFIG_PMAC_SMU
  1268. /* Probe SMU i2c busses */
  1269. smu_i2c_probe();
  1270. #endif
  1271. /* Now add plaform functions for some known devices */
  1272. pmac_i2c_devscan(pmac_i2c_dev_create);
  1273. return 0;
  1274. }
  1275. machine_arch_initcall(powermac, pmac_i2c_init);
  1276. /* Since pmac_i2c_init can be called too early for the platform device
  1277. * registration, we need to do it at a later time. In our case, subsys
  1278. * happens to fit well, though I agree it's a bit of a hack...
  1279. */
  1280. static int __init pmac_i2c_create_platform_devices(void)
  1281. {
  1282. struct pmac_i2c_bus *bus;
  1283. int i = 0;
  1284. /* In the case where we are initialized from smp_init(), we must
  1285. * not use the timer (and thus the irq). It's safe from now on
  1286. * though
  1287. */
  1288. pmac_i2c_force_poll = 0;
  1289. /* Create platform devices */
  1290. list_for_each_entry(bus, &pmac_i2c_busses, link) {
  1291. bus->platform_dev =
  1292. platform_device_alloc("i2c-powermac", i++);
  1293. if (bus->platform_dev == NULL)
  1294. return -ENOMEM;
  1295. bus->platform_dev->dev.platform_data = bus;
  1296. bus->platform_dev->dev.of_node = bus->busnode;
  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);