Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/power/tps65090-charger.c

  1 /*
  2  * Battery charger driver for TI's tps65090
  3  *
  4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
  5 
  6  * This program is free software; you can redistribute it and/or modify it
  7  * under the terms and conditions of the GNU General Public License,
  8  * version 2, as published by the Free Software Foundation.
  9 
 10  * This program is distributed in the hope it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 13  * more details.
 14 
 15  * You should have received a copy of the GNU General Public License
 16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  */
 18 #include <linux/delay.h>
 19 #include <linux/err.h>
 20 #include <linux/init.h>
 21 #include <linux/interrupt.h>
 22 #include <linux/kernel.h>
 23 #include <linux/module.h>
 24 #include <linux/of_device.h>
 25 #include <linux/platform_device.h>
 26 #include <linux/power_supply.h>
 27 #include <linux/slab.h>
 28 
 29 #include <linux/mfd/tps65090.h>
 30 
 31 #define TPS65090_CHARGER_ENABLE BIT(0)
 32 #define TPS65090_VACG           BIT(1)
 33 #define TPS65090_NOITERM        BIT(5)
 34 
 35 struct tps65090_charger {
 36         struct  device  *dev;
 37         int     ac_online;
 38         int     prev_ac_online;
 39         int     irq;
 40         struct power_supply     ac;
 41         struct tps65090_platform_data *pdata;
 42 };
 43 
 44 static enum power_supply_property tps65090_ac_props[] = {
 45         POWER_SUPPLY_PROP_ONLINE,
 46 };
 47 
 48 static int tps65090_low_chrg_current(struct tps65090_charger *charger)
 49 {
 50         int ret;
 51 
 52         ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL5,
 53                         TPS65090_NOITERM);
 54         if (ret < 0) {
 55                 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
 56                         __func__, TPS65090_REG_CG_CTRL5);
 57                 return ret;
 58         }
 59         return 0;
 60 }
 61 
 62 static int tps65090_enable_charging(struct tps65090_charger *charger)
 63 {
 64         int ret;
 65         uint8_t ctrl0 = 0;
 66 
 67         ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_CTRL0,
 68                             &ctrl0);
 69         if (ret < 0) {
 70                 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
 71                                 __func__, TPS65090_REG_CG_CTRL0);
 72                 return ret;
 73         }
 74 
 75         ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL0,
 76                                 (ctrl0 | TPS65090_CHARGER_ENABLE));
 77         if (ret < 0) {
 78                 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
 79                                 __func__, TPS65090_REG_CG_CTRL0);
 80                 return ret;
 81         }
 82         return 0;
 83 }
 84 
 85 static int tps65090_config_charger(struct tps65090_charger *charger)
 86 {
 87         uint8_t intrmask = 0;
 88         int ret;
 89 
 90         if (charger->pdata->enable_low_current_chrg) {
 91                 ret = tps65090_low_chrg_current(charger);
 92                 if (ret < 0) {
 93                         dev_err(charger->dev,
 94                                 "error configuring low charge current\n");
 95                         return ret;
 96                 }
 97         }
 98 
 99         /* Enable the VACG interrupt for AC power detect */
100         ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_MASK,
101                             &intrmask);
102         if (ret < 0) {
103                 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
104                         __func__, TPS65090_REG_INTR_MASK);
105                 return ret;
106         }
107 
108         ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_MASK,
109                              (intrmask | TPS65090_VACG));
110         if (ret < 0) {
111                 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
112                         __func__, TPS65090_REG_CG_CTRL0);
113                 return ret;
114         }
115 
116         return 0;
117 }
118 
119 static int tps65090_ac_get_property(struct power_supply *psy,
120                         enum power_supply_property psp,
121                         union power_supply_propval *val)
122 {
123         struct tps65090_charger *charger = container_of(psy,
124                                         struct tps65090_charger, ac);
125 
126         if (psp == POWER_SUPPLY_PROP_ONLINE) {
127                 val->intval = charger->ac_online;
128                 charger->prev_ac_online = charger->ac_online;
129                 return 0;
130         }
131         return -EINVAL;
132 }
133 
134 static irqreturn_t tps65090_charger_isr(int irq, void *dev_id)
135 {
136         struct tps65090_charger *charger = dev_id;
137         int ret;
138         uint8_t status1 = 0;
139         uint8_t intrsts = 0;
140 
141         ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_STATUS1,
142                             &status1);
143         if (ret < 0) {
144                 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
145                                 __func__, TPS65090_REG_CG_STATUS1);
146                 return IRQ_HANDLED;
147         }
148         msleep(75);
149         ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_STS,
150                             &intrsts);
151         if (ret < 0) {
152                 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
153                                 __func__, TPS65090_REG_INTR_STS);
154                 return IRQ_HANDLED;
155         }
156 
157         if (intrsts & TPS65090_VACG) {
158                 ret = tps65090_enable_charging(charger);
159                 if (ret < 0)
160                         return IRQ_HANDLED;
161                 charger->ac_online = 1;
162         } else {
163                 charger->ac_online = 0;
164         }
165 
166         /* Clear interrupts. */
167         ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_STS, 0x00);
168         if (ret < 0) {
169                 dev_err(charger->dev, "%s(): Error in writing reg 0x%x\n",
170                                 __func__, TPS65090_REG_INTR_STS);
171         }
172 
173         if (charger->prev_ac_online != charger->ac_online)
174                 power_supply_changed(&charger->ac);
175 
176         return IRQ_HANDLED;
177 }
178 
179 static struct tps65090_platform_data *
180                 tps65090_parse_dt_charger_data(struct platform_device *pdev)
181 {
182         struct tps65090_platform_data *pdata;
183         struct device_node *np = pdev->dev.of_node;
184         unsigned int prop;
185 
186         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
187         if (!pdata) {
188                 dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
189                 return NULL;
190         }
191 
192         prop = of_property_read_bool(np, "ti,enable-low-current-chrg");
193         pdata->enable_low_current_chrg = prop;
194 
195         pdata->irq_base = -1;
196 
197         return pdata;
198 
199 }
200 
201 static int tps65090_charger_probe(struct platform_device *pdev)
202 {
203         struct tps65090_charger *cdata;
204         struct tps65090_platform_data *pdata;
205         uint8_t status1 = 0;
206         int ret;
207         int irq;
208 
209         pdata = dev_get_platdata(pdev->dev.parent);
210 
211         if (IS_ENABLED(CONFIG_OF) && !pdata && pdev->dev.of_node)
212                 pdata = tps65090_parse_dt_charger_data(pdev);
213 
214         if (!pdata) {
215                 dev_err(&pdev->dev, "%s():no platform data available\n",
216                                 __func__);
217                 return -ENODEV;
218         }
219 
220         cdata = devm_kzalloc(&pdev->dev, sizeof(*cdata), GFP_KERNEL);
221         if (!cdata) {
222                 dev_err(&pdev->dev, "failed to allocate memory status\n");
223                 return -ENOMEM;
224         }
225 
226         platform_set_drvdata(pdev, cdata);
227 
228         cdata->dev                      = &pdev->dev;
229         cdata->pdata                    = pdata;
230 
231         cdata->ac.name                  = "tps65090-ac";
232         cdata->ac.type                  = POWER_SUPPLY_TYPE_MAINS;
233         cdata->ac.get_property          = tps65090_ac_get_property;
234         cdata->ac.properties            = tps65090_ac_props;
235         cdata->ac.num_properties        = ARRAY_SIZE(tps65090_ac_props);
236         cdata->ac.supplied_to           = pdata->supplied_to;
237         cdata->ac.num_supplicants       = pdata->num_supplicants;
238         cdata->ac.of_node               = pdev->dev.of_node;
239 
240         ret = power_supply_register(&pdev->dev, &cdata->ac);
241         if (ret) {
242                 dev_err(&pdev->dev, "failed: power supply register\n");
243                 return ret;
244         }
245 
246         irq = platform_get_irq(pdev, 0);
247         if (irq <= 0) {
248                 dev_warn(&pdev->dev, "Unable to get charger irq = %d\n", irq);
249                 ret = irq;
250                 goto fail_unregister_supply;
251         }
252 
253         cdata->irq = irq;
254 
255         ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
256                 tps65090_charger_isr, 0, "tps65090-charger", cdata);
257         if (ret) {
258                 dev_err(cdata->dev, "Unable to register irq %d err %d\n", irq,
259                         ret);
260                 goto fail_unregister_supply;
261         }
262 
263         ret = tps65090_config_charger(cdata);
264         if (ret < 0) {
265                 dev_err(&pdev->dev, "charger config failed, err %d\n", ret);
266                 goto fail_unregister_supply;
267         }
268 
269         /* Check for charger presence */
270         ret = tps65090_read(cdata->dev->parent, TPS65090_REG_CG_STATUS1,
271                         &status1);
272         if (ret < 0) {
273                 dev_err(cdata->dev, "%s(): Error in reading reg 0x%x", __func__,
274                         TPS65090_REG_CG_STATUS1);
275                 goto fail_unregister_supply;
276         }
277 
278         if (status1 != 0) {
279                 ret = tps65090_enable_charging(cdata);
280                 if (ret < 0) {
281                         dev_err(cdata->dev, "error enabling charger\n");
282                         goto fail_unregister_supply;
283                 }
284                 cdata->ac_online = 1;
285                 power_supply_changed(&cdata->ac);
286         }
287 
288         return 0;
289 
290 fail_unregister_supply:
291         power_supply_unregister(&cdata->ac);
292 
293         return ret;
294 }
295 
296 static int tps65090_charger_remove(struct platform_device *pdev)
297 {
298         struct tps65090_charger *cdata = platform_get_drvdata(pdev);
299 
300         power_supply_unregister(&cdata->ac);
301 
302         return 0;
303 }
304 
305 static struct of_device_id of_tps65090_charger_match[] = {
306         { .compatible = "ti,tps65090-charger", },
307         { /* end */ }
308 };
309 
310 static struct platform_driver tps65090_charger_driver = {
311         .driver = {
312                 .name   = "tps65090-charger",
313                 .of_match_table = of_tps65090_charger_match,
314                 .owner  = THIS_MODULE,
315         },
316         .probe  = tps65090_charger_probe,
317         .remove = tps65090_charger_remove,
318 };
319 module_platform_driver(tps65090_charger_driver);
320 
321 MODULE_LICENSE("GPL v2");
322 MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>");
323 MODULE_DESCRIPTION("tps65090 battery charger driver");
324 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us