aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/busses/i2c-stu300.c
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2012-07-16 07:30:12 -0400
committerWolfram Sang <w.sang@pengutronix.de>2012-07-16 07:48:51 -0400
commit6f535b94261b16343cfbc4576a941bd7901b96e1 (patch)
tree4dd6d6e3183c030728b5cdf330b4c9ffa1dd7607 /drivers/i2c/busses/i2c-stu300.c
parent7326e38ffe894d0cd2904704b7d8c53d4a55d752 (diff)
i2c: stu300: use devm managed resources
Allocate memory for device state using devm_kzalloc(), get the clock using devm_clk_get(), get the IRQ using devm_request_irq(), request and remap memory using devm_request_and_ioremap(). All to simplify accounting and letting the kernel do the garbage-collection. Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Wolfram Sang <w.sang@pengutronix.de>
Diffstat (limited to 'drivers/i2c/busses/i2c-stu300.c')
-rw-r--r--drivers/i2c/busses/i2c-stu300.c83
1 files changed, 18 insertions, 65 deletions
diff --git a/drivers/i2c/busses/i2c-stu300.c b/drivers/i2c/busses/i2c-stu300.c
index 80dd1c074a08..580a0c04cb42 100644
--- a/drivers/i2c/busses/i2c-stu300.c
+++ b/drivers/i2c/busses/i2c-stu300.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (C) 2007-2009 ST-Ericsson AB 2 * Copyright (C) 2007-2012 ST-Ericsson AB
3 * License terms: GNU General Public License (GPL) version 2 3 * License terms: GNU General Public License (GPL) version 2
4 * ST DDC I2C master mode driver, used in e.g. U300 series platforms. 4 * ST DDC I2C master mode driver, used in e.g. U300 series platforms.
5 * Author: Linus Walleij <linus.walleij@stericsson.com> 5 * Author: Linus Walleij <linus.walleij@stericsson.com>
@@ -139,8 +139,6 @@ module_param(scl_frequency, uint, 0644);
139 * struct stu300_dev - the stu300 driver state holder 139 * struct stu300_dev - the stu300 driver state holder
140 * @pdev: parent platform device 140 * @pdev: parent platform device
141 * @adapter: corresponding I2C adapter 141 * @adapter: corresponding I2C adapter
142 * @phybase: location of I/O area in memory
143 * @physize: size of I/O area in memory
144 * @clk: hardware block clock 142 * @clk: hardware block clock
145 * @irq: assigned interrupt line 143 * @irq: assigned interrupt line
146 * @cmd_issue_lock: this locks the following cmd_ variables 144 * @cmd_issue_lock: this locks the following cmd_ variables
@@ -155,8 +153,6 @@ module_param(scl_frequency, uint, 0644);
155struct stu300_dev { 153struct stu300_dev {
156 struct platform_device *pdev; 154 struct platform_device *pdev;
157 struct i2c_adapter adapter; 155 struct i2c_adapter adapter;
158 resource_size_t phybase;
159 resource_size_t physize;
160 void __iomem *virtbase; 156 void __iomem *virtbase;
161 struct clk *clk; 157 struct clk *clk;
162 int irq; 158 int irq;
@@ -873,64 +869,44 @@ stu300_probe(struct platform_device *pdev)
873 int ret = 0; 869 int ret = 0;
874 char clk_name[] = "I2C0"; 870 char clk_name[] = "I2C0";
875 871
876 dev = kzalloc(sizeof(struct stu300_dev), GFP_KERNEL); 872 dev = devm_kzalloc(&pdev->dev, sizeof(struct stu300_dev), GFP_KERNEL);
877 if (!dev) { 873 if (!dev) {
878 dev_err(&pdev->dev, "could not allocate device struct\n"); 874 dev_err(&pdev->dev, "could not allocate device struct\n");
879 ret = -ENOMEM; 875 return -ENOMEM;
880 goto err_no_devmem;
881 } 876 }
882 877
883 bus_nr = pdev->id; 878 bus_nr = pdev->id;
884 clk_name[3] += (char)bus_nr; 879 clk_name[3] += (char)bus_nr;
885 dev->clk = clk_get(&pdev->dev, clk_name); 880 dev->clk = devm_clk_get(&pdev->dev, clk_name);
886 if (IS_ERR(dev->clk)) { 881 if (IS_ERR(dev->clk)) {
887 ret = PTR_ERR(dev->clk);
888 dev_err(&pdev->dev, "could not retrieve i2c bus clock\n"); 882 dev_err(&pdev->dev, "could not retrieve i2c bus clock\n");
889 goto err_no_clk; 883 return PTR_ERR(dev->clk);
890 } 884 }
891 885
892 dev->pdev = pdev; 886 dev->pdev = pdev;
893 platform_set_drvdata(pdev, dev);
894
895 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 887 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
896 if (!res) { 888 if (!res)
897 ret = -ENOENT; 889 return -ENOENT;
898 goto err_no_resource;
899 }
900 890
901 dev->phybase = res->start; 891 dev->virtbase = devm_request_and_ioremap(&pdev->dev, res);
902 dev->physize = resource_size(res);
903
904 if (request_mem_region(dev->phybase, dev->physize,
905 NAME " I/O Area") == NULL) {
906 ret = -EBUSY;
907 goto err_no_ioregion;
908 }
909
910 dev->virtbase = ioremap(dev->phybase, dev->physize);
911 dev_dbg(&pdev->dev, "initialize bus device I2C%d on virtual " 892 dev_dbg(&pdev->dev, "initialize bus device I2C%d on virtual "
912 "base %p\n", bus_nr, dev->virtbase); 893 "base %p\n", bus_nr, dev->virtbase);
913 if (!dev->virtbase) { 894 if (!dev->virtbase)
914 ret = -ENOMEM; 895 return -ENOMEM;
915 goto err_no_ioremap;
916 }
917 896
918 dev->irq = platform_get_irq(pdev, 0); 897 dev->irq = platform_get_irq(pdev, 0);
919 if (request_irq(dev->irq, stu300_irh, 0, 898 ret = devm_request_irq(&pdev->dev, dev->irq, stu300_irh, 0, NAME, dev);
920 NAME, dev)) { 899 if (ret < 0)
921 ret = -EIO; 900 return ret;
922 goto err_no_irq;
923 }
924 901
925 dev->speed = scl_frequency; 902 dev->speed = scl_frequency;
926 903
927 clk_prepare_enable(dev->clk); 904 clk_prepare_enable(dev->clk);
928 ret = stu300_init_hw(dev); 905 ret = stu300_init_hw(dev);
929 clk_disable(dev->clk); 906 clk_disable(dev->clk);
930
931 if (ret != 0) { 907 if (ret != 0) {
932 dev_err(&dev->pdev->dev, "error initializing hardware.\n"); 908 dev_err(&dev->pdev->dev, "error initializing hardware.\n");
933 goto err_init_hw; 909 return -EIO;
934 } 910 }
935 911
936 /* IRQ event handling initialization */ 912 /* IRQ event handling initialization */
@@ -952,30 +928,13 @@ stu300_probe(struct platform_device *pdev)
952 /* i2c device drivers may be active on return from add_adapter() */ 928 /* i2c device drivers may be active on return from add_adapter() */
953 ret = i2c_add_numbered_adapter(adap); 929 ret = i2c_add_numbered_adapter(adap);
954 if (ret) { 930 if (ret) {
955 dev_err(&dev->pdev->dev, "failure adding ST Micro DDC " 931 dev_err(&pdev->dev, "failure adding ST Micro DDC "
956 "I2C adapter\n"); 932 "I2C adapter\n");
957 goto err_add_adapter; 933 return ret;
958 } 934 }
959 return 0;
960 935
961 err_add_adapter: 936 platform_set_drvdata(pdev, dev);
962 err_init_hw: 937 return 0;
963 clk_unprepare(dev->clk);
964 free_irq(dev->irq, dev);
965 err_no_irq:
966 iounmap(dev->virtbase);
967 err_no_ioremap:
968 release_mem_region(dev->phybase, dev->physize);
969 err_no_ioregion:
970 platform_set_drvdata(pdev, NULL);
971 err_no_resource:
972 clk_put(dev->clk);
973 err_no_clk:
974 kfree(dev);
975 err_no_devmem:
976 dev_err(&pdev->dev, "failed to add " NAME " adapter: %d\n",
977 pdev->id);
978 return ret;
979} 938}
980 939
981#ifdef CONFIG_PM 940#ifdef CONFIG_PM
@@ -1016,13 +975,7 @@ stu300_remove(struct platform_device *pdev)
1016 i2c_del_adapter(&dev->adapter); 975 i2c_del_adapter(&dev->adapter);
1017 /* Turn off everything */ 976 /* Turn off everything */
1018 stu300_wr8(0x00, dev->virtbase + I2C_CR); 977 stu300_wr8(0x00, dev->virtbase + I2C_CR);
1019 free_irq(dev->irq, dev);
1020 iounmap(dev->virtbase);
1021 release_mem_region(dev->phybase, dev->physize);
1022 clk_unprepare(dev->clk);
1023 clk_put(dev->clk);
1024 platform_set_drvdata(pdev, NULL); 978 platform_set_drvdata(pdev, NULL);
1025 kfree(dev);
1026 return 0; 979 return 0;
1027} 980}
1028 981