]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/leds/leds-lp55xx-common.c
leds-lp55xx: use lp55xx common init function - reset
[karo-tx-linux.git] / drivers / leds / leds-lp55xx-common.c
1 /*
2  * LP5521/LP5523/LP55231 Common Driver
3  *
4  * Copyright 2012 Texas Instruments
5  *
6  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * Derived from leds-lp5521.c, leds-lp5523.c
13  */
14
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/leds.h>
18 #include <linux/module.h>
19 #include <linux/platform_data/leds-lp55xx.h>
20
21 #include "leds-lp55xx-common.h"
22
23 static void lp55xx_reset_device(struct lp55xx_chip *chip)
24 {
25         struct lp55xx_device_config *cfg = chip->cfg;
26         u8 addr = cfg->reset.addr;
27         u8 val  = cfg->reset.val;
28
29         /* no error checking here because no ACK from the device after reset */
30         lp55xx_write(chip, addr, val);
31 }
32
33 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
34 {
35         return i2c_smbus_write_byte_data(chip->cl, reg, val);
36 }
37 EXPORT_SYMBOL_GPL(lp55xx_write);
38
39 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
40 {
41         s32 ret;
42
43         ret = i2c_smbus_read_byte_data(chip->cl, reg);
44         if (ret < 0)
45                 return ret;
46
47         *val = ret;
48         return 0;
49 }
50 EXPORT_SYMBOL_GPL(lp55xx_read);
51
52 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
53 {
54         int ret;
55         u8 tmp;
56
57         ret = lp55xx_read(chip, reg, &tmp);
58         if (ret)
59                 return ret;
60
61         tmp &= ~mask;
62         tmp |= val & mask;
63
64         return lp55xx_write(chip, reg, tmp);
65 }
66 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
67
68 int lp55xx_init_device(struct lp55xx_chip *chip)
69 {
70         struct lp55xx_platform_data *pdata;
71         struct lp55xx_device_config *cfg;
72         struct device *dev = &chip->cl->dev;
73         int ret = 0;
74
75         WARN_ON(!chip);
76
77         pdata = chip->pdata;
78         cfg = chip->cfg;
79
80         if (!pdata || !cfg)
81                 return -EINVAL;
82
83         if (pdata->setup_resources) {
84                 ret = pdata->setup_resources();
85                 if (ret < 0) {
86                         dev_err(dev, "setup resoure err: %d\n", ret);
87                         goto err;
88                 }
89         }
90
91         if (pdata->enable) {
92                 pdata->enable(0);
93                 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
94                 pdata->enable(1);
95                 usleep_range(1000, 2000); /* 500us abs min. */
96         }
97
98         lp55xx_reset_device(chip);
99
100         /*
101          * Exact value is not available. 10 - 20ms
102          * appears to be enough for reset.
103          */
104         usleep_range(10000, 20000);
105
106 err:
107         return ret;
108 }
109 EXPORT_SYMBOL_GPL(lp55xx_init_device);
110
111 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
112 MODULE_DESCRIPTION("LP55xx Common Driver");
113 MODULE_LICENSE("GPL");