blob: 6fede0b96715d2b85e4a1ca5cb907fbc24a696ab [file] [log] [blame]
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +09001/*
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
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +090015#include <linux/delay.h>
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +090016#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
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090023static 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
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +090033static 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
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +090056int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
57{
58 return i2c_smbus_write_byte_data(chip->cl, reg, val);
59}
60EXPORT_SYMBOL_GPL(lp55xx_write);
61
62int 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}
73EXPORT_SYMBOL_GPL(lp55xx_read);
74
75int 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}
89EXPORT_SYMBOL_GPL(lp55xx_update_bits);
90
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +090091int lp55xx_init_device(struct lp55xx_chip *chip)
92{
93 struct lp55xx_platform_data *pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090094 struct lp55xx_device_config *cfg;
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +090095 struct device *dev = &chip->cl->dev;
96 int ret = 0;
97
98 WARN_ON(!chip);
99
100 pdata = chip->pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900101 cfg = chip->cfg;
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900102
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900103 if (!pdata || !cfg)
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900104 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
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900121 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
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900129 ret = lp55xx_detect_device(chip);
130 if (ret) {
131 dev_err(dev, "device detection err: %d\n", ret);
132 goto err;
133 }
134
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900135err:
136 return ret;
137}
138EXPORT_SYMBOL_GPL(lp55xx_init_device);
139
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900140MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
141MODULE_DESCRIPTION("LP55xx Common Driver");
142MODULE_LICENSE("GPL");