low_i2c.c 37 KB

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