blob: 0d57c020087e36623aa4522abbe0965232c3215f [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* 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 Mucklef132c6c2012-06-06 18:30:57 -070018#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/init.h>
20#include <linux/bitops.h>
21#include <linux/mfd/pmic8058.h>
22#include <linux/regulator/driver.h>
Anirudh Ghayalc2019332011-11-12 06:29:10 +053023#include <linux/mfd/pm8xxx/core.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#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
43struct pm8058_xo_buffer {
Anirudh Ghayalc2019332011-11-12 06:29:10 +053044 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045 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
56static struct pm8058_xo_buffer pm8058_xo_buffer[] = {
57 XO_BUFFER(A0, 0x185),
58 XO_BUFFER(A1, 0x186),
59};
60
Anirudh Ghayalc2019332011-11-12 06:29:10 +053061static int pm8058_xo_buffer_write(struct pm8058_xo_buffer *xo,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062 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 Ghayalc2019332011-11-12 06:29:10 +053069 rc = pm8xxx_writeb(xo->dev->parent, addr, reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070
71 if (rc)
Anirudh Ghayalc2019332011-11-12 06:29:10 +053072 pr_err("FAIL: pm8xxx_write: rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073 else
74 *reg_save = reg;
75 return rc;
76}
77
78static int pm8058_xo_buffer_enable(struct regulator_dev *dev)
79{
80 struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 int rc;
82
Anirudh Ghayalc2019332011-11-12 06:29:10 +053083 rc = pm8058_xo_buffer_write(xo, xo->ctrl_addr, XO_ENABLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 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
91static 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
101static int pm8058_xo_buffer_disable(struct regulator_dev *dev)
102{
103 struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 int rc;
105
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530106 rc = pm8058_xo_buffer_write(xo, xo->ctrl_addr, XO_DISABLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 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
114static 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
129static 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 Ghayalc2019332011-11-12 06:29:10 +0530134static int pm8058_init_xo_buffer(struct pm8058_xo_buffer *xo)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135{
136 int rc;
137
138 /* Save the current control register state */
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530139 rc = pm8xxx_readb(xo->dev->parent, xo->ctrl_addr, &xo->ctrl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140
141 if (rc)
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530142 pr_err("FAIL: pm8xxx_read: rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 return rc;
144}
145
146static int __devinit pm8058_xo_buffer_probe(struct platform_device *pdev)
147{
148 struct regulator_desc *rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 rdesc = &pm8058_xo_buffer_desc[pdev->id];
157 xo = &pm8058_xo_buffer[pdev->id];
158 xo->pdata = pdev->dev.platform_data;
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530159 xo->dev = &pdev->dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530161 rc = pm8058_init_xo_buffer(xo);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 if (rc)
163 goto bail;
164
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 xo->rdev = regulator_register(rdesc, &pdev->dev,
Rajendra Nayak11eafc62011-11-18 16:47:19 +0530166 &xo->pdata->init_data, xo, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 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
176bail:
177 if (rc)
178 pr_err("Error: xo-id=%d, rc=%d\n", pdev->id, rc);
179
180 return rc;
181}
182
183static 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
189static 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
198static int __init pm8058_xo_buffer_init(void)
199{
200 return platform_driver_register(&pm8058_xo_buffer_driver);
201}
202
203static void __exit pm8058_xo_buffer_exit(void)
204{
205 platform_driver_unregister(&pm8058_xo_buffer_driver);
206}
207
208subsys_initcall(pm8058_xo_buffer_init);
209module_exit(pm8058_xo_buffer_exit);
210
211MODULE_LICENSE("GPL v2");
212MODULE_DESCRIPTION("PMIC8058 XO buffer driver");
213MODULE_VERSION("1.0");
214MODULE_ALIAS("platform:" PM8058_XO_BUFFER_DEV_NAME);