Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'i2c-for-linus' of git://aeryn.fluff.org.uk/bjdooks/linux

* 'i2c-for-linus' of git://aeryn.fluff.org.uk/bjdooks/linux:
i2c: Blackfin I2C Driver: Functional power management support
i2c: Documentation: upgrading clients HOWTO
i2c: S3C24XX I2C frequency scaling support.
i2c: i2c_gpio: keep probe resident for hotplugged devices.
i2c: S3C2410: Pass the I2C bus number via drivers platform data

+426 -29
+281
Documentation/i2c/upgrading-clients
··· 1 + Upgrading I2C Drivers to the new 2.6 Driver Model 2 + ================================================= 3 + 4 + Ben Dooks <ben-linux@fluff.org> 5 + 6 + Introduction 7 + ------------ 8 + 9 + This guide outlines how to alter existing Linux 2.6 client drivers from 10 + the old to the new new binding methods. 11 + 12 + 13 + Example old-style driver 14 + ------------------------ 15 + 16 + 17 + struct example_state { 18 + struct i2c_client client; 19 + .... 20 + }; 21 + 22 + static struct i2c_driver example_driver; 23 + 24 + static unsigned short ignore[] = { I2C_CLIENT_END }; 25 + static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END }; 26 + 27 + I2C_CLIENT_INSMOD; 28 + 29 + static int example_attach(struct i2c_adapter *adap, int addr, int kind) 30 + { 31 + struct example_state *state; 32 + struct device *dev = &adap->dev; /* to use for dev_ reports */ 33 + int ret; 34 + 35 + state = kzalloc(sizeof(struct example_state), GFP_KERNEL); 36 + if (state == NULL) { 37 + dev_err(dev, "failed to create our state\n"); 38 + return -ENOMEM; 39 + } 40 + 41 + example->client.addr = addr; 42 + example->client.flags = 0; 43 + example->client.adapter = adap; 44 + 45 + i2c_set_clientdata(&state->i2c_client, state); 46 + strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE); 47 + 48 + ret = i2c_attach_client(&state->i2c_client); 49 + if (ret < 0) { 50 + dev_err(dev, "failed to attach client\n"); 51 + kfree(state); 52 + return ret; 53 + } 54 + 55 + dev = &state->i2c_client.dev; 56 + 57 + /* rest of the initialisation goes here. */ 58 + 59 + dev_info(dev, "example client created\n"); 60 + 61 + return 0; 62 + } 63 + 64 + static int __devexit example_detach(struct i2c_client *client) 65 + { 66 + struct example_state *state = i2c_get_clientdata(client); 67 + 68 + i2c_detach_client(client); 69 + kfree(state); 70 + return 0; 71 + } 72 + 73 + static int example_attach_adapter(struct i2c_adapter *adap) 74 + { 75 + return i2c_probe(adap, &addr_data, example_attach); 76 + } 77 + 78 + static struct i2c_driver example_driver = { 79 + .driver = { 80 + .owner = THIS_MODULE, 81 + .name = "example", 82 + }, 83 + .attach_adapter = example_attach_adapter, 84 + .detach_client = __devexit_p(example_detach), 85 + .suspend = example_suspend, 86 + .resume = example_resume, 87 + }; 88 + 89 + 90 + Updating the client 91 + ------------------- 92 + 93 + The new style binding model will check against a list of supported 94 + devices and their associated address supplied by the code registering 95 + the busses. This means that the driver .attach_adapter and 96 + .detach_adapter methods can be removed, along with the addr_data, 97 + as follows: 98 + 99 + - static struct i2c_driver example_driver; 100 + 101 + - static unsigned short ignore[] = { I2C_CLIENT_END }; 102 + - static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END }; 103 + 104 + - I2C_CLIENT_INSMOD; 105 + 106 + - static int example_attach_adapter(struct i2c_adapter *adap) 107 + - { 108 + - return i2c_probe(adap, &addr_data, example_attach); 109 + - } 110 + 111 + static struct i2c_driver example_driver = { 112 + - .attach_adapter = example_attach_adapter, 113 + - .detach_client = __devexit_p(example_detach), 114 + } 115 + 116 + Add the probe and remove methods to the i2c_driver, as so: 117 + 118 + static struct i2c_driver example_driver = { 119 + + .probe = example_probe, 120 + + .remove = __devexit_p(example_remove), 121 + } 122 + 123 + Change the example_attach method to accept the new parameters 124 + which include the i2c_client that it will be working with: 125 + 126 + - static int example_attach(struct i2c_adapter *adap, int addr, int kind) 127 + + static int example_probe(struct i2c_client *client, 128 + + const struct i2c_device_id *id) 129 + 130 + Change the name of example_attach to example_probe to align it with the 131 + i2c_driver entry names. The rest of the probe routine will now need to be 132 + changed as the i2c_client has already been setup for use. 133 + 134 + The necessary client fields have already been setup before 135 + the probe function is called, so the following client setup 136 + can be removed: 137 + 138 + - example->client.addr = addr; 139 + - example->client.flags = 0; 140 + - example->client.adapter = adap; 141 + - 142 + - strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE); 143 + 144 + The i2c_set_clientdata is now: 145 + 146 + - i2c_set_clientdata(&state->client, state); 147 + + i2c_set_clientdata(client, state); 148 + 149 + The call to i2c_attach_client is no longer needed, if the probe 150 + routine exits successfully, then the driver will be automatically 151 + attached by the core. Change the probe routine as so: 152 + 153 + - ret = i2c_attach_client(&state->i2c_client); 154 + - if (ret < 0) { 155 + - dev_err(dev, "failed to attach client\n"); 156 + - kfree(state); 157 + - return ret; 158 + - } 159 + 160 + 161 + Remove the storage of 'struct i2c_client' from the 'struct example_state' 162 + as we are provided with the i2c_client in our example_probe. Instead we 163 + store a pointer to it for when it is needed. 164 + 165 + struct example_state { 166 + - struct i2c_client client; 167 + + struct i2c_client *client; 168 + 169 + the new i2c client as so: 170 + 171 + - struct device *dev = &adap->dev; /* to use for dev_ reports */ 172 + + struct device *dev = &i2c_client->dev; /* to use for dev_ reports */ 173 + 174 + And remove the change after our client is attached, as the driver no 175 + longer needs to register a new client structure with the core: 176 + 177 + - dev = &state->i2c_client.dev; 178 + 179 + In the probe routine, ensure that the new state has the client stored 180 + in it: 181 + 182 + static int example_probe(struct i2c_client *i2c_client, 183 + const struct i2c_device_id *id) 184 + { 185 + struct example_state *state; 186 + struct device *dev = &i2c_client->dev; 187 + int ret; 188 + 189 + state = kzalloc(sizeof(struct example_state), GFP_KERNEL); 190 + if (state == NULL) { 191 + dev_err(dev, "failed to create our state\n"); 192 + return -ENOMEM; 193 + } 194 + 195 + + state->client = i2c_client; 196 + 197 + Update the detach method, by changing the name to _remove and 198 + to delete the i2c_detach_client call. It is possible that you 199 + can also remove the ret variable as it is not not needed for 200 + any of the core functions. 201 + 202 + - static int __devexit example_detach(struct i2c_client *client) 203 + + static int __devexit example_remove(struct i2c_client *client) 204 + { 205 + struct example_state *state = i2c_get_clientdata(client); 206 + 207 + - i2c_detach_client(client); 208 + 209 + And finally ensure that we have the correct ID table for the i2c-core 210 + and other utilities: 211 + 212 + + struct i2c_device_id example_idtable[] = { 213 + + { "example", 0 }, 214 + + { } 215 + +}; 216 + + 217 + +MODULE_DEVICE_TABLE(i2c, example_idtable); 218 + 219 + static struct i2c_driver example_driver = { 220 + .driver = { 221 + .owner = THIS_MODULE, 222 + .name = "example", 223 + }, 224 + + .id_table = example_ids, 225 + 226 + 227 + Our driver should now look like this: 228 + 229 + struct example_state { 230 + struct i2c_client *client; 231 + .... 232 + }; 233 + 234 + static int example_probe(struct i2c_client *client, 235 + const struct i2c_device_id *id) 236 + { 237 + struct example_state *state; 238 + struct device *dev = &client->dev; 239 + 240 + state = kzalloc(sizeof(struct example_state), GFP_KERNEL); 241 + if (state == NULL) { 242 + dev_err(dev, "failed to create our state\n"); 243 + return -ENOMEM; 244 + } 245 + 246 + state->client = client; 247 + i2c_set_clientdata(client, state); 248 + 249 + /* rest of the initialisation goes here. */ 250 + 251 + dev_info(dev, "example client created\n"); 252 + 253 + return 0; 254 + } 255 + 256 + static int __devexit example_remove(struct i2c_client *client) 257 + { 258 + struct example_state *state = i2c_get_clientdata(client); 259 + 260 + kfree(state); 261 + return 0; 262 + } 263 + 264 + static struct i2c_device_id example_idtable[] = { 265 + { "example", 0 }, 266 + { } 267 + }; 268 + 269 + MODULE_DEVICE_TABLE(i2c, example_idtable); 270 + 271 + static struct i2c_driver example_driver = { 272 + .driver = { 273 + .owner = THIS_MODULE, 274 + .name = "example", 275 + }, 276 + .id_table = example_idtable, 277 + .probe = example_probe, 278 + .remove = __devexit_p(example_remove), 279 + .suspend = example_suspend, 280 + .resume = example_resume, 281 + };
+24 -11
drivers/i2c/busses/i2c-bfin-twi.c
··· 49 49 struct i2c_msg *pmsg; 50 50 int msg_num; 51 51 int cur_msg; 52 + u16 saved_clkdiv; 53 + u16 saved_control; 52 54 void __iomem *regs_base; 53 55 }; 54 56 ··· 567 565 I2C_FUNC_I2C; 568 566 } 569 567 570 - 571 568 static struct i2c_algorithm bfin_twi_algorithm = { 572 569 .master_xfer = bfin_twi_master_xfer, 573 570 .smbus_xfer = bfin_twi_smbus_xfer, 574 571 .functionality = bfin_twi_functionality, 575 572 }; 576 573 577 - 578 - static int i2c_bfin_twi_suspend(struct platform_device *dev, pm_message_t state) 574 + static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state) 579 575 { 580 - struct bfin_twi_iface *iface = platform_get_drvdata(dev); 576 + struct bfin_twi_iface *iface = platform_get_drvdata(pdev); 577 + 578 + iface->saved_clkdiv = read_CLKDIV(iface); 579 + iface->saved_control = read_CONTROL(iface); 580 + 581 + free_irq(iface->irq, iface); 581 582 582 583 /* Disable TWI */ 583 - write_CONTROL(iface, read_CONTROL(iface) & ~TWI_ENA); 584 - SSYNC(); 584 + write_CONTROL(iface, iface->saved_control & ~TWI_ENA); 585 585 586 586 return 0; 587 587 } 588 588 589 - static int i2c_bfin_twi_resume(struct platform_device *dev) 589 + static int i2c_bfin_twi_resume(struct platform_device *pdev) 590 590 { 591 - struct bfin_twi_iface *iface = platform_get_drvdata(dev); 591 + struct bfin_twi_iface *iface = platform_get_drvdata(pdev); 592 592 593 - /* Enable TWI */ 594 - write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA); 595 - SSYNC(); 593 + int rc = request_irq(iface->irq, bfin_twi_interrupt_entry, 594 + IRQF_DISABLED, pdev->name, iface); 595 + if (rc) { 596 + dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq); 597 + return -ENODEV; 598 + } 599 + 600 + /* Resume TWI interface clock as specified */ 601 + write_CLKDIV(iface, iface->saved_clkdiv); 602 + 603 + /* Resume TWI */ 604 + write_CONTROL(iface, iface->saved_control); 596 605 597 606 return 0; 598 607 }
+5 -4
drivers/i2c/busses/i2c-gpio.c
··· 77 77 return gpio_get_value(pdata->scl_pin); 78 78 } 79 79 80 - static int __init i2c_gpio_probe(struct platform_device *pdev) 80 + static int __devinit i2c_gpio_probe(struct platform_device *pdev) 81 81 { 82 82 struct i2c_gpio_platform_data *pdata; 83 83 struct i2c_algo_bit_data *bit_data; ··· 174 174 return ret; 175 175 } 176 176 177 - static int __exit i2c_gpio_remove(struct platform_device *pdev) 177 + static int __devexit i2c_gpio_remove(struct platform_device *pdev) 178 178 { 179 179 struct i2c_gpio_platform_data *pdata; 180 180 struct i2c_adapter *adap; ··· 196 196 .name = "i2c-gpio", 197 197 .owner = THIS_MODULE, 198 198 }, 199 - .remove = __exit_p(i2c_gpio_remove), 199 + .probe = i2c_gpio_probe, 200 + .remove = __devexit_p(i2c_gpio_remove), 200 201 }; 201 202 202 203 static int __init i2c_gpio_init(void) 203 204 { 204 205 int ret; 205 206 206 - ret = platform_driver_probe(&i2c_gpio_driver, i2c_gpio_probe); 207 + ret = platform_driver_register(&i2c_gpio_driver); 207 208 if (ret) 208 209 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); 209 210
+115 -14
drivers/i2c/busses/i2c-s3c2410.c
··· 33 33 #include <linux/err.h> 34 34 #include <linux/platform_device.h> 35 35 #include <linux/clk.h> 36 + #include <linux/cpufreq.h> 36 37 37 38 #include <asm/hardware.h> 38 39 #include <asm/irq.h> ··· 65 64 unsigned int tx_setup; 66 65 67 66 enum s3c24xx_i2c_state state; 67 + unsigned long clkrate; 68 68 69 69 void __iomem *regs; 70 70 struct clk *clk; ··· 73 71 struct resource *irq; 74 72 struct resource *ioarea; 75 73 struct i2c_adapter adap; 74 + 75 + #ifdef CONFIG_CPU_FREQ 76 + struct notifier_block freq_transition; 77 + #endif 76 78 }; 77 79 78 80 /* default platform data to use if not supplied in the platform_device ··· 507 501 unsigned long timeout; 508 502 int ret; 509 503 504 + if (!readl(i2c->regs + S3C2410_IICCON) & S3C2410_IICCON_IRQEN) 505 + return -EIO; 506 + 510 507 ret = s3c24xx_i2c_set_master(i2c); 511 508 if (ret != 0) { 512 509 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); ··· 645 636 return (diff >= -2 && diff <= 2); 646 637 } 647 638 648 - /* s3c24xx_i2c_getdivisor 639 + /* s3c24xx_i2c_clockrate 649 640 * 650 641 * work out a divisor for the user requested frequency setting, 651 642 * either by the requested frequency, or scanning the acceptable 652 643 * range of frequencies until something is found 653 644 */ 654 645 655 - static int s3c24xx_i2c_getdivisor(struct s3c24xx_i2c *i2c, 656 - struct s3c2410_platform_i2c *pdata, 657 - unsigned long *iicon, 658 - unsigned int *got) 646 + static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 659 647 { 648 + struct s3c2410_platform_i2c *pdata; 660 649 unsigned long clkin = clk_get_rate(i2c->clk); 661 - 662 650 unsigned int divs, div1; 651 + u32 iiccon; 663 652 int freq; 664 653 int start, end; 665 654 655 + i2c->clkrate = clkin; 656 + 657 + pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent); 666 658 clkin /= 1000; /* clkin now in KHz */ 667 659 668 - dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n", 660 + dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n", 669 661 pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq); 670 662 671 663 if (pdata->bus_freq != 0) { ··· 698 688 699 689 found: 700 690 *got = freq; 701 - *iicon |= (divs-1); 702 - *iicon |= (div1 == 512) ? S3C2410_IICCON_TXDIV_512 : 0; 691 + 692 + iiccon = readl(i2c->regs + S3C2410_IICCON); 693 + iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); 694 + iiccon |= (divs-1); 695 + 696 + if (div1 == 512) 697 + iiccon |= S3C2410_IICCON_TXDIV_512; 698 + 699 + writel(iiccon, i2c->regs + S3C2410_IICCON); 700 + 703 701 return 0; 704 702 } 703 + 704 + #ifdef CONFIG_CPU_FREQ 705 + 706 + #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) 707 + 708 + static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, 709 + unsigned long val, void *data) 710 + { 711 + struct s3c24xx_i2c *i2c = freq_to_i2c(nb); 712 + unsigned long flags; 713 + unsigned int got; 714 + int delta_f; 715 + int ret; 716 + 717 + delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; 718 + 719 + /* if we're post-change and the input clock has slowed down 720 + * or at pre-change and the clock is about to speed up, then 721 + * adjust our clock rate. <0 is slow, >0 speedup. 722 + */ 723 + 724 + if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || 725 + (val == CPUFREQ_PRECHANGE && delta_f > 0)) { 726 + spin_lock_irqsave(&i2c->lock, flags); 727 + ret = s3c24xx_i2c_clockrate(i2c, &got); 728 + spin_unlock_irqrestore(&i2c->lock, flags); 729 + 730 + if (ret < 0) 731 + dev_err(i2c->dev, "cannot find frequency\n"); 732 + else 733 + dev_info(i2c->dev, "setting freq %d\n", got); 734 + } 735 + 736 + return 0; 737 + } 738 + 739 + static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 740 + { 741 + i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; 742 + 743 + return cpufreq_register_notifier(&i2c->freq_transition, 744 + CPUFREQ_TRANSITION_NOTIFIER); 745 + } 746 + 747 + static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 748 + { 749 + cpufreq_unregister_notifier(&i2c->freq_transition, 750 + CPUFREQ_TRANSITION_NOTIFIER); 751 + } 752 + 753 + #else 754 + static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 755 + { 756 + return 0; 757 + } 758 + 759 + static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 760 + { 761 + } 762 + #endif 705 763 706 764 /* s3c24xx_i2c_init 707 765 * ··· 797 719 798 720 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); 799 721 722 + writel(iicon, i2c->regs + S3C2410_IICCON); 723 + 800 724 /* we need to work out the divisors for the clock... */ 801 725 802 - if (s3c24xx_i2c_getdivisor(i2c, pdata, &iicon, &freq) != 0) { 726 + if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { 727 + writel(0, i2c->regs + S3C2410_IICCON); 803 728 dev_err(i2c->dev, "cannot meet bus frequency required\n"); 804 729 return -EINVAL; 805 730 } ··· 811 730 812 731 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); 813 732 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); 814 - 815 - writel(iicon, i2c->regs + S3C2410_IICCON); 816 733 817 734 /* check for s3c2440 i2c controller */ 818 735 ··· 831 752 static int s3c24xx_i2c_probe(struct platform_device *pdev) 832 753 { 833 754 struct s3c24xx_i2c *i2c = &s3c24xx_i2c; 755 + struct s3c2410_platform_i2c *pdata; 834 756 struct resource *res; 835 757 int ret; 758 + 759 + pdata = s3c24xx_i2c_get_platformdata(&pdev->dev); 836 760 837 761 /* find the clock and enable it */ 838 762 ··· 914 832 dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res, 915 833 (unsigned long)res->start); 916 834 917 - ret = i2c_add_adapter(&i2c->adap); 835 + ret = s3c24xx_i2c_register_cpufreq(i2c); 836 + if (ret < 0) { 837 + dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); 838 + goto err_irq; 839 + } 840 + 841 + /* Note, previous versions of the driver used i2c_add_adapter() 842 + * to add the bus at any number. We now pass the bus number via 843 + * the platform data, so if unset it will now default to always 844 + * being bus 0. 845 + */ 846 + 847 + i2c->adap.nr = pdata->bus_num; 848 + 849 + ret = i2c_add_numbered_adapter(&i2c->adap); 918 850 if (ret < 0) { 919 851 dev_err(&pdev->dev, "failed to add bus to i2c core\n"); 920 - goto err_irq; 852 + goto err_cpufreq; 921 853 } 922 854 923 855 platform_set_drvdata(pdev, i2c); 924 856 925 857 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id); 926 858 return 0; 859 + 860 + err_cpufreq: 861 + s3c24xx_i2c_deregister_cpufreq(i2c); 927 862 928 863 err_irq: 929 864 free_irq(i2c->irq->start, i2c); ··· 968 869 static int s3c24xx_i2c_remove(struct platform_device *pdev) 969 870 { 970 871 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 872 + 873 + s3c24xx_i2c_deregister_cpufreq(i2c); 971 874 972 875 i2c_del_adapter(&i2c->adap); 973 876 free_irq(i2c->irq->start, i2c);
+1
include/asm-arm/plat-s3c/iic.h
··· 21 21 */ 22 22 23 23 struct s3c2410_platform_i2c { 24 + int bus_num; /* bus number to use */ 24 25 unsigned int flags; 25 26 unsigned int slave_addr; /* slave address for controller */ 26 27 unsigned long bus_freq; /* standard bus frequency */