]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/leds/leds-lp55xx-common.c
leds-lp55xx: use lp55xx common init function - detect
[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 static int lp55xx_detect_device(struct lp55xx_chip *chip)
34 {
35         struct lp55xx_device_config *cfg = chip->cfg;
36         u8 addr = cfg->enable.addr;
37         u8 val  = cfg->enable.val;
38         int ret;
39
40         ret = lp55xx_write(chip, addr, val);
41         if (ret)
42                 return ret;
43
44         usleep_range(1000, 2000);
45
46         ret = lp55xx_read(chip, addr, &val);
47         if (ret)
48                 return ret;
49
50         if (val != cfg->enable.val)
51                 return -ENODEV;
52
53         return 0;
54 }
55
56 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
57 {
58         return i2c_smbus_write_byte_data(chip->cl, reg, val);
59 }
60 EXPORT_SYMBOL_GPL(lp55xx_write);
61
62 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
63 {
64         s32 ret;
65
66         ret = i2c_smbus_read_byte_data(chip->cl, reg);
67         if (ret < 0)
68                 return ret;
69
70         *val = ret;
71         return 0;
72 }
73 EXPORT_SYMBOL_GPL(lp55xx_read);
74
75 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
76 {
77         int ret;
78         u8 tmp;
79
80         ret = lp55xx_read(chip, reg, &tmp);
81         if (ret)
82                 return ret;
83
84         tmp &= ~mask;
85         tmp |= val & mask;
86
87         return lp55xx_write(chip, reg, tmp);
88 }
89 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
90
91 int lp55xx_init_device(struct lp55xx_chip *chip)
92 {
93         struct lp55xx_platform_data *pdata;
94         struct lp55xx_device_config *cfg;
95         struct device *dev = &chip->cl->dev;
96         int ret = 0;
97
98         WARN_ON(!chip);
99
100         pdata = chip->pdata;
101         cfg = chip->cfg;
102
103         if (!pdata || !cfg)
104                 return -EINVAL;
105
106         if (pdata->setup_resources) {
107                 ret = pdata->setup_resources();
108                 if (ret < 0) {
109                         dev_err(dev, "setup resoure err: %d\n", ret);
110                         goto err;
111                 }
112         }
113
114         if (pdata->enable) {
115                 pdata->enable(0);
116                 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
117                 pdata->enable(1);
118                 usleep_range(1000, 2000); /* 500us abs min. */
119         }
120
121         lp55xx_reset_device(chip);
122
123         /*
124          * Exact value is not available. 10 - 20ms
125          * appears to be enough for reset.
126          */
127         usleep_range(10000, 20000);
128
129         ret = lp55xx_detect_device(chip);
130         if (ret) {
131                 dev_err(dev, "device detection err: %d\n", ret);
132                 goto err;
133         }
134
135 err:
136         return ret;
137 }
138 EXPORT_SYMBOL_GPL(lp55xx_init_device);
139
140 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
141 MODULE_DESCRIPTION("LP55xx Common Driver");
142 MODULE_LICENSE("GPL");