Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/kernel.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/mfd/pmic8058.h> |
| 22 | #include <linux/regulator/driver.h> |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 23 | #include <linux/mfd/pm8xxx/core.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include <linux/regulator/pm8058-xo.h> |
| 25 | |
| 26 | /* XO buffer masks and values */ |
| 27 | |
| 28 | #define XO_PULLDOWN_MASK 0x08 |
| 29 | #define XO_PULLDOWN_ENABLE 0x08 |
| 30 | #define XO_PULLDOWN_DISABLE 0x00 |
| 31 | |
| 32 | #define XO_BUFFER_MASK 0x04 |
| 33 | #define XO_BUFFER_ENABLE 0x04 |
| 34 | #define XO_BUFFER_DISABLE 0x00 |
| 35 | |
| 36 | #define XO_MODE_MASK 0x01 |
| 37 | #define XO_MODE_MANUAL 0x00 |
| 38 | |
| 39 | #define XO_ENABLE_MASK (XO_MODE_MASK | XO_BUFFER_MASK) |
| 40 | #define XO_ENABLE (XO_MODE_MANUAL | XO_BUFFER_ENABLE) |
| 41 | #define XO_DISABLE (XO_MODE_MANUAL | XO_BUFFER_DISABLE) |
| 42 | |
| 43 | struct pm8058_xo_buffer { |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 44 | struct device *dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | struct pm8058_xo_pdata *pdata; |
| 46 | struct regulator_dev *rdev; |
| 47 | u16 ctrl_addr; |
| 48 | u8 ctrl_reg; |
| 49 | }; |
| 50 | |
| 51 | #define XO_BUFFER(_id, _ctrl_addr) \ |
| 52 | [PM8058_XO_ID_##_id] = { \ |
| 53 | .ctrl_addr = _ctrl_addr, \ |
| 54 | } |
| 55 | |
| 56 | static struct pm8058_xo_buffer pm8058_xo_buffer[] = { |
| 57 | XO_BUFFER(A0, 0x185), |
| 58 | XO_BUFFER(A1, 0x186), |
| 59 | }; |
| 60 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 61 | static int pm8058_xo_buffer_write(struct pm8058_xo_buffer *xo, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 62 | u16 addr, u8 val, u8 mask, u8 *reg_save) |
| 63 | { |
| 64 | u8 reg; |
| 65 | int rc = 0; |
| 66 | |
| 67 | reg = (*reg_save & ~mask) | (val & mask); |
| 68 | if (reg != *reg_save) |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 69 | rc = pm8xxx_writeb(xo->dev->parent, addr, reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 70 | |
| 71 | if (rc) |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 72 | pr_err("FAIL: pm8xxx_write: rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 73 | else |
| 74 | *reg_save = reg; |
| 75 | return rc; |
| 76 | } |
| 77 | |
| 78 | static int pm8058_xo_buffer_enable(struct regulator_dev *dev) |
| 79 | { |
| 80 | struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | int rc; |
| 82 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 83 | rc = pm8058_xo_buffer_write(xo, xo->ctrl_addr, XO_ENABLE, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 84 | XO_ENABLE_MASK, &xo->ctrl_reg); |
| 85 | if (rc) |
| 86 | pr_err("FAIL: pm8058_xo_buffer_write: rc=%d\n", rc); |
| 87 | |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | static int pm8058_xo_buffer_is_enabled(struct regulator_dev *dev) |
| 92 | { |
| 93 | struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev); |
| 94 | |
| 95 | if (xo->ctrl_reg & XO_BUFFER_ENABLE) |
| 96 | return 1; |
| 97 | else |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int pm8058_xo_buffer_disable(struct regulator_dev *dev) |
| 102 | { |
| 103 | struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | int rc; |
| 105 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 106 | rc = pm8058_xo_buffer_write(xo, xo->ctrl_addr, XO_DISABLE, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | XO_ENABLE_MASK, &xo->ctrl_reg); |
| 108 | if (rc) |
| 109 | pr_err("FAIL: pm8058_xo_buffer_write: rc=%d\n", rc); |
| 110 | |
| 111 | return rc; |
| 112 | } |
| 113 | |
| 114 | static struct regulator_ops pm8058_xo_ops = { |
| 115 | .enable = pm8058_xo_buffer_enable, |
| 116 | .disable = pm8058_xo_buffer_disable, |
| 117 | .is_enabled = pm8058_xo_buffer_is_enabled, |
| 118 | }; |
| 119 | |
| 120 | #define VREG_DESCRIP(_id, _name, _ops) \ |
| 121 | [_id] = { \ |
| 122 | .id = _id, \ |
| 123 | .name = _name, \ |
| 124 | .ops = _ops, \ |
| 125 | .type = REGULATOR_VOLTAGE, \ |
| 126 | .owner = THIS_MODULE, \ |
| 127 | } |
| 128 | |
| 129 | static struct regulator_desc pm8058_xo_buffer_desc[] = { |
| 130 | VREG_DESCRIP(PM8058_XO_ID_A0, "8058_xo_a0", &pm8058_xo_ops), |
| 131 | VREG_DESCRIP(PM8058_XO_ID_A1, "8058_xo_a1", &pm8058_xo_ops), |
| 132 | }; |
| 133 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 134 | static int pm8058_init_xo_buffer(struct pm8058_xo_buffer *xo) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 135 | { |
| 136 | int rc; |
| 137 | |
| 138 | /* Save the current control register state */ |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 139 | rc = pm8xxx_readb(xo->dev->parent, xo->ctrl_addr, &xo->ctrl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | |
| 141 | if (rc) |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 142 | pr_err("FAIL: pm8xxx_read: rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 143 | return rc; |
| 144 | } |
| 145 | |
| 146 | static int __devinit pm8058_xo_buffer_probe(struct platform_device *pdev) |
| 147 | { |
| 148 | struct regulator_desc *rdesc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 149 | struct pm8058_xo_buffer *xo; |
| 150 | int rc = 0; |
| 151 | |
| 152 | if (pdev == NULL) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | if (pdev->id >= 0 && pdev->id < PM8058_XO_ID_MAX) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | rdesc = &pm8058_xo_buffer_desc[pdev->id]; |
| 157 | xo = &pm8058_xo_buffer[pdev->id]; |
| 158 | xo->pdata = pdev->dev.platform_data; |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 159 | xo->dev = &pdev->dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 161 | rc = pm8058_init_xo_buffer(xo); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 162 | if (rc) |
| 163 | goto bail; |
| 164 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 165 | xo->rdev = regulator_register(rdesc, &pdev->dev, |
Rajendra Nayak | 11eafc6 | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 166 | &xo->pdata->init_data, xo, NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | if (IS_ERR(xo->rdev)) { |
| 168 | rc = PTR_ERR(xo->rdev); |
| 169 | pr_err("FAIL: regulator_register(%s): rc=%d\n", |
| 170 | pm8058_xo_buffer_desc[pdev->id].name, rc); |
| 171 | } |
| 172 | } else { |
| 173 | rc = -ENODEV; |
| 174 | } |
| 175 | |
| 176 | bail: |
| 177 | if (rc) |
| 178 | pr_err("Error: xo-id=%d, rc=%d\n", pdev->id, rc); |
| 179 | |
| 180 | return rc; |
| 181 | } |
| 182 | |
| 183 | static int __devexit pm8058_xo_buffer_remove(struct platform_device *pdev) |
| 184 | { |
| 185 | regulator_unregister(pm8058_xo_buffer[pdev->id].rdev); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static struct platform_driver pm8058_xo_buffer_driver = { |
| 190 | .probe = pm8058_xo_buffer_probe, |
| 191 | .remove = __devexit_p(pm8058_xo_buffer_remove), |
| 192 | .driver = { |
| 193 | .name = PM8058_XO_BUFFER_DEV_NAME, |
| 194 | .owner = THIS_MODULE, |
| 195 | }, |
| 196 | }; |
| 197 | |
| 198 | static int __init pm8058_xo_buffer_init(void) |
| 199 | { |
| 200 | return platform_driver_register(&pm8058_xo_buffer_driver); |
| 201 | } |
| 202 | |
| 203 | static void __exit pm8058_xo_buffer_exit(void) |
| 204 | { |
| 205 | platform_driver_unregister(&pm8058_xo_buffer_driver); |
| 206 | } |
| 207 | |
| 208 | subsys_initcall(pm8058_xo_buffer_init); |
| 209 | module_exit(pm8058_xo_buffer_exit); |
| 210 | |
| 211 | MODULE_LICENSE("GPL v2"); |
| 212 | MODULE_DESCRIPTION("PMIC8058 XO buffer driver"); |
| 213 | MODULE_VERSION("1.0"); |
| 214 | MODULE_ALIAS("platform:" PM8058_XO_BUFFER_DEV_NAME); |