UPSTREAM: regulator: core: use dev_to_rdev
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / rk29-pwm-regulator.c
index aa49b3f799cce252727aa18e6600848723e0b40a..ce5f56127ef4aa7ccf028ecf5df912745bbae291 100644 (file)
@@ -1,52 +1,52 @@
 /* drivers/regulator/rk29-pwm-regulator.c\r
- *
- * Copyright (C) 2010 ROCKCHIP, Inc.
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- */
-/*******************************************************************/
-/*       COPYRIGHT (C)  ROCK-CHIPS FUZHOU . ALL RIGHTS RESERVED.                         */
-/*******************************************************************
+ *\r
+ * Copyright (C) 2010 ROCKCHIP, Inc.\r
+ *\r
+ * This software is licensed under the terms of the GNU General Public\r
+ * License version 2, as published by the Free Software Foundation, and\r
+ * may be copied, distributed, and modified under those terms.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ */\r
+/*******************************************************************/\r
+/*       COPYRIGHT (C)  ROCK-CHIPS FUZHOU . ALL RIGHTS RESERVED.                         */\r
+/*******************************************************************\r
 FILE           :               rk29-pwm-regulator.c\r
 DESC           :       rk29 pwm regulator driver\r
 AUTHOR         :       hxy\r
 DATE           :       2010-12-20\r
-NOTES          :
-$LOG: GPIO.C,V $
-REVISION 0.01
-********************************************************************/
-
-
-#include <linux/bug.h>
+NOTES          :\r
+$LOG: GPIO.C,V $\r
+REVISION 0.01\r
+********************************************************************/\r
+\r
+\r
+#include <linux/bug.h>\r
 #include <linux/err.h>\r
 #include <linux/platform_device.h>\r
 #include <linux/kernel.h>\r
 #include <linux/clk.h>\r
 #include <asm/io.h>\r
 #include <linux/delay.h>\r
-#include <linux/regulator/driver.h>
+#include <linux/regulator/driver.h>\r
 #include <linux/regulator/rk29-pwm-regulator.h>\r
 #include <mach/iomux.h>\r
 #include <linux/gpio.h>\r
-
-
-#if 0
-#define DBG(x...)      printk(KERN_INFO x)
-#else
-#define DBG(x...)
-#endif
 \r
 \r
-#define        PWM_VCORE_120           40\r
-#define        PWM_VCORE_125           32\r
+#if 0\r
+#define DBG(x...)      printk(KERN_INFO x)\r
+#else\r
+#define DBG(x...)\r
+#endif\r
+\r
+\r
+#define        PWM_VCORE_120           40\r
+#define PWM_VCORE_125          32\r
 #define        PWM_VCORE_130           21\r
 #define        PWM_VCORE_135           10\r
 #define        PWM_VCORE_140           0\r
@@ -64,53 +64,53 @@ static struct clk *pwm_clk;
 \r
 static int pwm_set_rate(struct pwm_platform_data *pdata,int nHz,u32 rate)\r
 {\r
-    u32 divh,divTotal;\r
-    int id = pdata->pwm_id;\r
-    unsigned long clkrate;\r
-\r
-    clkrate = clk_get_rate(pwm_clk);\r
-       \r
-    if ( id >3 || id <0 )\r
-       return -1;\r
-\r
-    if(rate == 0)\r
-       {\r
-        // iomux pwm2 to gpio2_a[3]     \r
-        rk29_mux_api_set(pdata->pwm_iomux_name, pdata->pwm_iomux_gpio);\r
-        // set gpio to low level        \r
-        gpio_set_value(pdata->pwm_gpio,GPIO_LOW);\r
+       u32 divh,divTotal;\r
+       int id = pdata->pwm_id;\r
+       unsigned long clkrate;\r
+\r
+       clkrate = clk_get_rate(pwm_clk);\r
+\r
+       if ( id >3 || id <0 )\r
+               return -1;\r
+\r
+       if(rate == 0)\r
+       {\r
+               // iomux pwm2 to gpio2_a[3]\r
+               rk29_mux_api_set(pdata->pwm_iomux_name, pdata->pwm_iomux_gpio);\r
+               // set gpio to low level\r
+               gpio_set_value(pdata->pwm_gpio,GPIO_LOW);\r
        }\r
-    else if (rate <= 100)\r
-       {\r
-        // iomux pwm2           \r
-        rk29_mux_api_set(pdata->pwm_iomux_name, pdata->pwm_iomux_pwm);\r
-                \r
-        pwm_write_reg(id,PWM_REG_CTRL, PWM_DIV|PWM_RESET);\r
-        divh = clkrate / nHz;\r
-        divh = divh >> (1+(PWM_DIV>>9));\r
-        pwm_write_reg(id,PWM_REG_LRC,(divh == 0)?1:divh);\r
-                \r
-        divTotal =pwm_read_reg(id,PWM_REG_LRC);\r
-        divh = divTotal*rate/100;\r
-        pwm_write_reg(id, PWM_REG_HRC, divh?divh:1);\r
-        pwm_write_reg(id,PWM_REG_CNTR,0);\r
-        pwm_write_reg(id, PWM_REG_CTRL,pwm_read_reg(id,PWM_REG_CTRL)|PWM_DIV|PWM_ENABLE|PWM_TimeEN);\r
+       else if (rate <= 100)\r
+       {\r
+               // iomux pwm2\r
+               rk29_mux_api_set(pdata->pwm_iomux_name, pdata->pwm_iomux_pwm);\r
+\r
+               pwm_write_reg(id,PWM_REG_CTRL, PWM_DIV|PWM_RESET);\r
+               divh = clkrate / nHz;\r
+               divh = divh >> (1+(PWM_DIV>>9));\r
+               pwm_write_reg(id,PWM_REG_LRC,(divh == 0)?1:divh);\r
+\r
+               divTotal =pwm_read_reg(id,PWM_REG_LRC);\r
+               divh = divTotal*rate/100;\r
+               pwm_write_reg(id, PWM_REG_HRC, divh?divh:1);\r
+               pwm_write_reg(id,PWM_REG_CNTR,0);\r
+               pwm_write_reg(id, PWM_REG_CTRL,pwm_read_reg(id,PWM_REG_CTRL)|PWM_DIV|PWM_ENABLE|PWM_TimeEN);\r
        }\r
-     else\r
-       {\r
-       return -1;\r
+       else\r
+       {\r
+               return -1;\r
        }\r
 \r
        usleep_range(10*1000, 10*1000);\r
-       \r
-       \r
-    return (0);\r
+\r
+\r
+       return (0);\r
 }\r
 \r
 static int pwm_regulator_list_voltage(struct regulator_dev *dev,unsigned int index)\r
 {\r
-       DBG("Enter %s, index =%d\n",__FUNCTION__,index);\r
-       if (index < sizeof(pwm_voltage_map)/sizeof(int))\r
+       DBG("Enter %s, index =%d\n",__FUNCTION__,index);\r
+       if (index < sizeof(pwm_voltage_map)/sizeof(int))\r
                return pwm_voltage_map[index];\r
        else\r
                return -1;\r
@@ -120,67 +120,76 @@ static int pwm_regulator_is_enabled(struct regulator_dev *dev)
 {\r
        DBG("Enter %s\n",__FUNCTION__);\r
        return 0;\r
-}
-
+}\r
+\r
 static int pwm_regulator_enable(struct regulator_dev *dev)\r
 {\r
        DBG("Enter %s\n",__FUNCTION__);\r
        return 0;\r
-}
-
+}\r
+\r
 static int pwm_regulator_disable(struct regulator_dev *dev)\r
 {\r
        DBG("Enter %s\n",__FUNCTION__);\r
        return 0;\r
-}
-
+}\r
+\r
 static int pwm_regulator_get_voltage(struct regulator_dev *dev)\r
 {\r
-       struct pwm_platform_data *pdata = rdev_get_drvdata(dev);\r
-          \r
+       struct pwm_platform_data *pdata = rdev_get_drvdata(dev);\r
+\r
        DBG("Enter %s\n",__FUNCTION__);  \r
-       \r
+\r
        return (pdata->pwm_voltage*1000);\r
-}
-
+}\r
+\r
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38))\r
+static int pwm_regulator_set_voltage(struct regulator_dev *dev,\r
+               int min_uV, int max_uV, unsigned *selector)\r
+#else\r
 static int pwm_regulator_set_voltage(struct regulator_dev *dev,\r
-                                 int min_uV, int max_uV)\r
+               int min_uV, int max_uV)\r
+#endif\r
 {         \r
        struct pwm_platform_data *pdata = rdev_get_drvdata(dev);\r
-       \r
-       const int *voltage_map = pwm_voltage_map;\r
-          \r
-       int min_mV = min_uV /1000, max_mA = max_uV / 1000;\r
-          \r
+\r
+       const int *voltage_map = pwm_voltage_map;\r
+\r
+       int min_mV = min_uV /1000, max_mA = max_uV / 1000;\r
+\r
        u32 size = sizeof(pwm_voltage_map)/sizeof(int), i, vol,pwm_value;\r
 \r
        DBG("%s:  min_uV = %d, max_uV = %d\n",__FUNCTION__, min_uV,max_uV);\r
 \r
        if (min_mV < voltage_map[0] ||max_mA > voltage_map[size-1])\r
-               return -EINVAL;
-          
+               return -EINVAL;\r
+\r
        for (i = 0; i < size; i++)\r
        {\r
                if (voltage_map[i] >= min_mV)\r
-                       break;
+                       break;\r
        }\r
-
 \r
-       vol =  voltage_map[i];\r
 \r
-       pdata->pwm_voltage = vol;\r
-          \r
+       vol =  voltage_map[i];\r
+\r
+       pdata->pwm_voltage = vol;\r
+\r
        // VDD12 = 1.4 - 0.476*D , ÆäÖÐDΪPWMÕ¼¿Õ±È, \r
        pwm_value = 100*(1400-vol)/476;  // pwm_value %\r
 \r
-          \r
+\r
        if (pwm_set_rate(pdata,1000*1000,pwm_value)!=0)\r
                return -1;\r
 \r
-       return 0;\r
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38))\r
+       *selector = i;\r
+#endif\r
+\r
+       return 0;\r
+\r
+}\r
 \r
-}
-
 static struct regulator_ops pwm_voltage_ops = {\r
        .list_voltage   = pwm_regulator_list_voltage,\r
        .set_voltage    =pwm_regulator_set_voltage,\r
@@ -189,39 +198,39 @@ static struct regulator_ops pwm_voltage_ops = {
        .disable        = pwm_regulator_disable,\r
        .is_enabled     = pwm_regulator_is_enabled,\r
 };\r
-
+\r
 static struct regulator_desc pwm_regulator= {\r
-               .name = "pwm-regulator",\r
-               .ops = &pwm_voltage_ops,\r
-               .type = REGULATOR_VOLTAGE,\r
+       .name = "pwm-regulator",\r
+       .ops = &pwm_voltage_ops,\r
+       .type = REGULATOR_VOLTAGE,\r
 };\r
-
+\r
 static int __devinit pwm_regulator_probe(struct platform_device *pdev)\r
 {\r
 \r
        struct pwm_platform_data *pdata = pdev->dev.platform_data;\r
        struct regulator_dev *rdev;\r
        int ret ;\r
-
-       if (!pdata)
-               return -ENODEV;
-
-       if (!pdata->pwm_voltage)
-               pdata->pwm_voltage = 1200;      // default 1.2v
-
+\r
+       if (!pdata)\r
+               return -ENODEV;\r
+\r
+       if (!pdata->pwm_voltage)\r
+               pdata->pwm_voltage = 1200;      // default 1.2v\r
+\r
        rdev = regulator_register(&pwm_regulator, &pdev->dev,\r
-                               pdata->init_data, pdata);\r
+                       pdata->init_data, pdata);\r
        if (IS_ERR(rdev)) {\r
-               dev_dbg(&pdev->dev, "couldn't register regulator\n");
+               dev_dbg(&pdev->dev, "couldn't register regulator\n");\r
                return PTR_ERR(rdev);\r
        }\r
-       \r
-       ret = gpio_request(pdata->pwm_gpio,"pwm");\r
+\r
+       ret = gpio_request(pdata->pwm_gpio,"pwm");\r
 \r
        if (ret) {\r
-                       dev_err(&pdev->dev,"failed to request pwm gpio\n");\r
-                       goto err_gpio;\r
-               }\r
+               dev_err(&pdev->dev,"failed to request pwm gpio\n");\r
+               goto err_gpio;\r
+       }\r
 \r
        pwm_clk = clk_get(NULL, "pwm");\r
        clk_enable(pwm_clk);\r
@@ -232,14 +241,14 @@ static int __devinit pwm_regulator_probe(struct platform_device *pdev)
 \r
        return 0;\r
 \r
-       \r
+\r
 err_gpio:\r
        gpio_free(pdata->pwm_gpio);\r
 \r
 \r
        return ret;\r
 \r
-}
+}\r
 static int __devexit pwm_regulator_remove(struct platform_device *pdev)\r
 {\r
        struct pwm_platform_data *pdata = pdev->dev.platform_data;\r
@@ -257,23 +266,23 @@ static struct platform_driver pwm_regulator_driver = {
        },\r
        .remove = __devexit_p(pwm_regulator_remove),\r
 };\r
-
+\r
 \r
 static int __init pwm_regulator_module_init(void)\r
-{
+{\r
        return platform_driver_probe(&pwm_regulator_driver, pwm_regulator_probe);\r
-}
-
+}\r
+\r
 static void __exit pwm_regulator_module_exit(void)\r
-{
+{\r
        platform_driver_unregister(&pwm_regulator_driver);\r
-}
+}\r
 \r
 \r
 subsys_initcall(pwm_regulator_module_init);\r
 \r
 module_exit(pwm_regulator_module_exit);\r
-
-MODULE_LICENSE("GPL");
+\r
+MODULE_LICENSE("GPL");\r
 MODULE_AUTHOR("hxy <hxy@rock-chips.com>");\r
 MODULE_DESCRIPTION("k29 pwm change driver");\r