diff options
author | Jon Smirl <jonsmirl@gmail.com> | 2008-06-30 19:01:26 -0400 |
---|---|---|
committer | Grant Likely <grant.likely@secretlab.ca> | 2008-07-12 14:10:23 -0400 |
commit | 0d1cde235874b00905bce23f659690d060ebf475 (patch) | |
tree | 918cad848eb4d85a3ac909666b5639ff618b2bdc | |
parent | 6eb9d32298290b956693fd85c815b817d39a9505 (diff) |
powerpc/i2c: Convert i2c-mpc into an of_platform driver
Convert i2c-mpc to an of_platform driver. Utilize the code in
drivers/of-i2c.c to make i2c modules dynamically loadable by the
device tree.
Signed-off-by: Jon Smirl <jonsmirl@gmail.com>
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
-rw-r--r-- | arch/powerpc/sysdev/fsl_soc.c | 133 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-mpc.c | 104 |
2 files changed, 60 insertions, 177 deletions
diff --git a/arch/powerpc/sysdev/fsl_soc.c b/arch/powerpc/sysdev/fsl_soc.c index ca54563d5c7e..ebcec7362f95 100644 --- a/arch/powerpc/sysdev/fsl_soc.c +++ b/arch/powerpc/sysdev/fsl_soc.c | |||
@@ -414,139 +414,6 @@ err: | |||
414 | 414 | ||
415 | arch_initcall(gfar_of_init); | 415 | arch_initcall(gfar_of_init); |
416 | 416 | ||
417 | #ifdef CONFIG_I2C_BOARDINFO | ||
418 | #include <linux/i2c.h> | ||
419 | struct i2c_driver_device { | ||
420 | char *of_device; | ||
421 | char *i2c_type; | ||
422 | }; | ||
423 | |||
424 | static struct i2c_driver_device i2c_devices[] __initdata = { | ||
425 | {"ricoh,rs5c372a", "rs5c372a"}, | ||
426 | {"ricoh,rs5c372b", "rs5c372b"}, | ||
427 | {"ricoh,rv5c386", "rv5c386"}, | ||
428 | {"ricoh,rv5c387a", "rv5c387a"}, | ||
429 | {"dallas,ds1307", "ds1307"}, | ||
430 | {"dallas,ds1337", "ds1337"}, | ||
431 | {"dallas,ds1338", "ds1338"}, | ||
432 | {"dallas,ds1339", "ds1339"}, | ||
433 | {"dallas,ds1340", "ds1340"}, | ||
434 | {"stm,m41t00", "m41t00"}, | ||
435 | {"dallas,ds1374", "ds1374"}, | ||
436 | {"cirrus,cs4270", "cs4270"}, | ||
437 | }; | ||
438 | |||
439 | static int __init of_find_i2c_driver(struct device_node *node, | ||
440 | struct i2c_board_info *info) | ||
441 | { | ||
442 | int i; | ||
443 | |||
444 | for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) { | ||
445 | if (!of_device_is_compatible(node, i2c_devices[i].of_device)) | ||
446 | continue; | ||
447 | if (strlcpy(info->type, i2c_devices[i].i2c_type, | ||
448 | I2C_NAME_SIZE) >= I2C_NAME_SIZE) | ||
449 | return -ENOMEM; | ||
450 | return 0; | ||
451 | } | ||
452 | |||
453 | pr_warning("fsl_soc.c: unrecognized i2c node %s\n", | ||
454 | (const char *) of_get_property(node, "compatible", NULL)); | ||
455 | |||
456 | return -ENODEV; | ||
457 | } | ||
458 | |||
459 | static void __init of_register_i2c_devices(struct device_node *adap_node, | ||
460 | int bus_num) | ||
461 | { | ||
462 | struct device_node *node = NULL; | ||
463 | |||
464 | while ((node = of_get_next_child(adap_node, node))) { | ||
465 | struct i2c_board_info info = {}; | ||
466 | const u32 *addr; | ||
467 | int len; | ||
468 | |||
469 | addr = of_get_property(node, "reg", &len); | ||
470 | if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) { | ||
471 | printk(KERN_WARNING "fsl_soc.c: invalid i2c device entry\n"); | ||
472 | continue; | ||
473 | } | ||
474 | |||
475 | info.irq = irq_of_parse_and_map(node, 0); | ||
476 | if (info.irq == NO_IRQ) | ||
477 | info.irq = -1; | ||
478 | |||
479 | if (of_find_i2c_driver(node, &info) < 0) | ||
480 | continue; | ||
481 | |||
482 | info.addr = *addr; | ||
483 | |||
484 | i2c_register_board_info(bus_num, &info, 1); | ||
485 | } | ||
486 | } | ||
487 | |||
488 | static int __init fsl_i2c_of_init(void) | ||
489 | { | ||
490 | struct device_node *np; | ||
491 | unsigned int i = 0; | ||
492 | struct platform_device *i2c_dev; | ||
493 | int ret; | ||
494 | |||
495 | for_each_compatible_node(np, NULL, "fsl-i2c") { | ||
496 | struct resource r[2]; | ||
497 | struct fsl_i2c_platform_data i2c_data; | ||
498 | const unsigned char *flags = NULL; | ||
499 | int idx; | ||
500 | const u32 *iprop; | ||
501 | |||
502 | memset(&r, 0, sizeof(r)); | ||
503 | memset(&i2c_data, 0, sizeof(i2c_data)); | ||
504 | |||
505 | ret = of_address_to_resource(np, 0, &r[0]); | ||
506 | if (ret) | ||
507 | goto err; | ||
508 | |||
509 | of_irq_to_resource(np, 0, &r[1]); | ||
510 | |||
511 | iprop = of_get_property(np, "cell-index", NULL); | ||
512 | idx = iprop ? *iprop : i; | ||
513 | |||
514 | i2c_dev = platform_device_register_simple("fsl-i2c", idx, r, 2); | ||
515 | if (IS_ERR(i2c_dev)) { | ||
516 | ret = PTR_ERR(i2c_dev); | ||
517 | goto err; | ||
518 | } | ||
519 | |||
520 | i2c_data.device_flags = 0; | ||
521 | flags = of_get_property(np, "dfsrr", NULL); | ||
522 | if (flags) | ||
523 | i2c_data.device_flags |= FSL_I2C_DEV_SEPARATE_DFSRR; | ||
524 | |||
525 | flags = of_get_property(np, "fsl5200-clocking", NULL); | ||
526 | if (flags) | ||
527 | i2c_data.device_flags |= FSL_I2C_DEV_CLOCK_5200; | ||
528 | |||
529 | ret = | ||
530 | platform_device_add_data(i2c_dev, &i2c_data, | ||
531 | sizeof(struct | ||
532 | fsl_i2c_platform_data)); | ||
533 | if (ret) | ||
534 | goto unreg; | ||
535 | |||
536 | of_register_i2c_devices(np, idx); | ||
537 | i++; | ||
538 | } | ||
539 | |||
540 | return 0; | ||
541 | |||
542 | unreg: | ||
543 | platform_device_unregister(i2c_dev); | ||
544 | err: | ||
545 | return ret; | ||
546 | } | ||
547 | |||
548 | arch_initcall(fsl_i2c_of_init); | ||
549 | #endif | ||
550 | 417 | ||
551 | #ifdef CONFIG_PPC_83xx | 418 | #ifdef CONFIG_PPC_83xx |
552 | static int __init mpc83xx_wdt_init(void) | 419 | static int __init mpc83xx_wdt_init(void) |
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c index a076129de7e8..4fdfb62d6f38 100644 --- a/drivers/i2c/busses/i2c-mpc.c +++ b/drivers/i2c/busses/i2c-mpc.c | |||
@@ -17,7 +17,8 @@ | |||
17 | #include <linux/module.h> | 17 | #include <linux/module.h> |
18 | #include <linux/sched.h> | 18 | #include <linux/sched.h> |
19 | #include <linux/init.h> | 19 | #include <linux/init.h> |
20 | #include <linux/platform_device.h> | 20 | #include <linux/of_platform.h> |
21 | #include <linux/of_i2c.h> | ||
21 | 22 | ||
22 | #include <asm/io.h> | 23 | #include <asm/io.h> |
23 | #include <linux/fsl_devices.h> | 24 | #include <linux/fsl_devices.h> |
@@ -25,13 +26,13 @@ | |||
25 | #include <linux/interrupt.h> | 26 | #include <linux/interrupt.h> |
26 | #include <linux/delay.h> | 27 | #include <linux/delay.h> |
27 | 28 | ||
28 | #define MPC_I2C_ADDR 0x00 | 29 | #define DRV_NAME "mpc-i2c" |
30 | |||
29 | #define MPC_I2C_FDR 0x04 | 31 | #define MPC_I2C_FDR 0x04 |
30 | #define MPC_I2C_CR 0x08 | 32 | #define MPC_I2C_CR 0x08 |
31 | #define MPC_I2C_SR 0x0c | 33 | #define MPC_I2C_SR 0x0c |
32 | #define MPC_I2C_DR 0x10 | 34 | #define MPC_I2C_DR 0x10 |
33 | #define MPC_I2C_DFSRR 0x14 | 35 | #define MPC_I2C_DFSRR 0x14 |
34 | #define MPC_I2C_REGION 0x20 | ||
35 | 36 | ||
36 | #define CCR_MEN 0x80 | 37 | #define CCR_MEN 0x80 |
37 | #define CCR_MIEN 0x40 | 38 | #define CCR_MIEN 0x40 |
@@ -315,102 +316,117 @@ static struct i2c_adapter mpc_ops = { | |||
315 | .timeout = 1, | 316 | .timeout = 1, |
316 | }; | 317 | }; |
317 | 318 | ||
318 | static int fsl_i2c_probe(struct platform_device *pdev) | 319 | static int __devinit fsl_i2c_probe(struct of_device *op, const struct of_device_id *match) |
319 | { | 320 | { |
320 | int result = 0; | 321 | int result = 0; |
321 | struct mpc_i2c *i2c; | 322 | struct mpc_i2c *i2c; |
322 | struct fsl_i2c_platform_data *pdata; | ||
323 | struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | ||
324 | |||
325 | pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data; | ||
326 | 323 | ||
327 | i2c = kzalloc(sizeof(*i2c), GFP_KERNEL); | 324 | i2c = kzalloc(sizeof(*i2c), GFP_KERNEL); |
328 | if (!i2c) | 325 | if (!i2c) |
329 | return -ENOMEM; | 326 | return -ENOMEM; |
330 | 327 | ||
331 | i2c->irq = platform_get_irq(pdev, 0); | 328 | if (of_get_property(op->node, "dfsrr", NULL)) |
332 | if (i2c->irq < 0) | 329 | i2c->flags |= FSL_I2C_DEV_SEPARATE_DFSRR; |
333 | i2c->irq = NO_IRQ; /* Use polling */ | ||
334 | 330 | ||
335 | i2c->flags = pdata->device_flags; | 331 | if (of_device_is_compatible(op->node, "fsl,mpc5200-i2c") || |
336 | init_waitqueue_head(&i2c->queue); | 332 | of_device_is_compatible(op->node, "mpc5200-i2c")) |
333 | i2c->flags |= FSL_I2C_DEV_CLOCK_5200; | ||
337 | 334 | ||
338 | i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION); | 335 | init_waitqueue_head(&i2c->queue); |
339 | 336 | ||
337 | i2c->base = of_iomap(op->node, 0); | ||
340 | if (!i2c->base) { | 338 | if (!i2c->base) { |
341 | printk(KERN_ERR "i2c-mpc - failed to map controller\n"); | 339 | printk(KERN_ERR "i2c-mpc - failed to map controller\n"); |
342 | result = -ENOMEM; | 340 | result = -ENOMEM; |
343 | goto fail_map; | 341 | goto fail_map; |
344 | } | 342 | } |
345 | 343 | ||
346 | if (i2c->irq != NO_IRQ) | 344 | i2c->irq = irq_of_parse_and_map(op->node, 0); |
347 | if ((result = request_irq(i2c->irq, mpc_i2c_isr, | 345 | if (i2c->irq != NO_IRQ) { /* i2c->irq = NO_IRQ implies polling */ |
348 | IRQF_SHARED, "i2c-mpc", i2c)) < 0) { | 346 | result = request_irq(i2c->irq, mpc_i2c_isr, |
349 | printk(KERN_ERR | 347 | IRQF_SHARED, "i2c-mpc", i2c); |
350 | "i2c-mpc - failed to attach interrupt\n"); | 348 | if (result < 0) { |
351 | goto fail_irq; | 349 | printk(KERN_ERR "i2c-mpc - failed to attach interrupt\n"); |
350 | goto fail_request; | ||
352 | } | 351 | } |
353 | 352 | } | |
353 | |||
354 | mpc_i2c_setclock(i2c); | 354 | mpc_i2c_setclock(i2c); |
355 | platform_set_drvdata(pdev, i2c); | 355 | |
356 | dev_set_drvdata(&op->dev, i2c); | ||
356 | 357 | ||
357 | i2c->adap = mpc_ops; | 358 | i2c->adap = mpc_ops; |
358 | i2c->adap.nr = pdev->id; | ||
359 | i2c_set_adapdata(&i2c->adap, i2c); | 359 | i2c_set_adapdata(&i2c->adap, i2c); |
360 | i2c->adap.dev.parent = &pdev->dev; | 360 | i2c->adap.dev.parent = &op->dev; |
361 | if ((result = i2c_add_numbered_adapter(&i2c->adap)) < 0) { | 361 | |
362 | result = i2c_add_adapter(&i2c->adap); | ||
363 | if (result < 0) { | ||
362 | printk(KERN_ERR "i2c-mpc - failed to add adapter\n"); | 364 | printk(KERN_ERR "i2c-mpc - failed to add adapter\n"); |
363 | goto fail_add; | 365 | goto fail_add; |
364 | } | 366 | } |
367 | of_register_i2c_devices(&i2c->adap, op->node); | ||
365 | 368 | ||
366 | return result; | 369 | return result; |
367 | 370 | ||
368 | fail_add: | 371 | fail_add: |
369 | if (i2c->irq != NO_IRQ) | 372 | dev_set_drvdata(&op->dev, NULL); |
370 | free_irq(i2c->irq, i2c); | 373 | free_irq(i2c->irq, i2c); |
371 | fail_irq: | 374 | fail_request: |
372 | iounmap(i2c->base); | 375 | irq_dispose_mapping(i2c->irq); |
373 | fail_map: | 376 | iounmap(i2c->base); |
377 | fail_map: | ||
374 | kfree(i2c); | 378 | kfree(i2c); |
375 | return result; | 379 | return result; |
376 | }; | 380 | }; |
377 | 381 | ||
378 | static int fsl_i2c_remove(struct platform_device *pdev) | 382 | static int __devexit fsl_i2c_remove(struct of_device *op) |
379 | { | 383 | { |
380 | struct mpc_i2c *i2c = platform_get_drvdata(pdev); | 384 | struct mpc_i2c *i2c = dev_get_drvdata(&op->dev); |
381 | 385 | ||
382 | i2c_del_adapter(&i2c->adap); | 386 | i2c_del_adapter(&i2c->adap); |
383 | platform_set_drvdata(pdev, NULL); | 387 | dev_set_drvdata(&op->dev, NULL); |
384 | 388 | ||
385 | if (i2c->irq != NO_IRQ) | 389 | if (i2c->irq != NO_IRQ) |
386 | free_irq(i2c->irq, i2c); | 390 | free_irq(i2c->irq, i2c); |
387 | 391 | ||
392 | irq_dispose_mapping(i2c->irq); | ||
388 | iounmap(i2c->base); | 393 | iounmap(i2c->base); |
389 | kfree(i2c); | 394 | kfree(i2c); |
390 | return 0; | 395 | return 0; |
391 | }; | 396 | }; |
392 | 397 | ||
393 | /* work with hotplug and coldplug */ | 398 | static const struct of_device_id mpc_i2c_of_match[] = { |
394 | MODULE_ALIAS("platform:fsl-i2c"); | 399 | {.compatible = "fsl-i2c",}, |
400 | {}, | ||
401 | }; | ||
402 | MODULE_DEVICE_TABLE(of, mpc_i2c_of_match); | ||
403 | |||
395 | 404 | ||
396 | /* Structure for a device driver */ | 405 | /* Structure for a device driver */ |
397 | static struct platform_driver fsl_i2c_driver = { | 406 | static struct of_platform_driver mpc_i2c_driver = { |
398 | .probe = fsl_i2c_probe, | 407 | .match_table = mpc_i2c_of_match, |
399 | .remove = fsl_i2c_remove, | 408 | .probe = fsl_i2c_probe, |
400 | .driver = { | 409 | .remove = __devexit_p(fsl_i2c_remove), |
401 | .owner = THIS_MODULE, | 410 | .driver = { |
402 | .name = "fsl-i2c", | 411 | .owner = THIS_MODULE, |
412 | .name = DRV_NAME, | ||
403 | }, | 413 | }, |
404 | }; | 414 | }; |
405 | 415 | ||
406 | static int __init fsl_i2c_init(void) | 416 | static int __init fsl_i2c_init(void) |
407 | { | 417 | { |
408 | return platform_driver_register(&fsl_i2c_driver); | 418 | int rv; |
419 | |||
420 | rv = of_register_platform_driver(&mpc_i2c_driver); | ||
421 | if (rv) | ||
422 | printk(KERN_ERR DRV_NAME | ||
423 | " of_register_platform_driver failed (%i)\n", rv); | ||
424 | return rv; | ||
409 | } | 425 | } |
410 | 426 | ||
411 | static void __exit fsl_i2c_exit(void) | 427 | static void __exit fsl_i2c_exit(void) |
412 | { | 428 | { |
413 | platform_driver_unregister(&fsl_i2c_driver); | 429 | of_unregister_platform_driver(&mpc_i2c_driver); |
414 | } | 430 | } |
415 | 431 | ||
416 | module_init(fsl_i2c_init); | 432 | module_init(fsl_i2c_init); |