blob: 5c80aea3211fde53e1f8d37b47c69e6319cf3809 [file] [log] [blame]
Haojian Zhuangd50f8f32010-01-08 12:29:23 +01001/*
2 * I2C driver for Maxim MAX8925
3 *
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
Paul Gortmaker9eb5f9b2016-06-02 20:39:46 -040012#include <linux/init.h>
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010013#include <linux/platform_device.h>
14#include <linux/i2c.h>
15#include <linux/mfd/max8925.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010017
Haojian Zhuangb13c0df2010-01-25 06:26:34 -050018#define RTC_I2C_ADDR 0x68
19#define ADC_I2C_ADDR 0x47
20
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010021static inline int max8925_read_device(struct i2c_client *i2c,
22 int reg, int bytes, void *dest)
23{
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010024 int ret;
25
Haojian Zhuangb13c0df2010-01-25 06:26:34 -050026 if (bytes > 1)
27 ret = i2c_smbus_read_i2c_block_data(i2c, reg, bytes, dest);
28 else {
29 ret = i2c_smbus_read_byte_data(i2c, reg);
30 if (ret < 0)
31 return ret;
32 *(unsigned char *)dest = (unsigned char)ret;
33 }
34 return ret;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010035}
36
37static inline int max8925_write_device(struct i2c_client *i2c,
38 int reg, int bytes, void *src)
39{
Lee Jones87bd1c92014-08-28 11:01:15 +010040 unsigned char buf[9];
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010041 int ret;
42
43 buf[0] = (unsigned char)reg;
44 memcpy(&buf[1], src, bytes);
45
46 ret = i2c_master_send(i2c, buf, bytes + 1);
47 if (ret < 0)
48 return ret;
49 return 0;
50}
51
52int max8925_reg_read(struct i2c_client *i2c, int reg)
53{
54 struct max8925_chip *chip = i2c_get_clientdata(i2c);
Haojian Zhuangb13c0df2010-01-25 06:26:34 -050055 unsigned char data = 0;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010056 int ret;
57
58 mutex_lock(&chip->io_lock);
59 ret = max8925_read_device(i2c, reg, 1, &data);
60 mutex_unlock(&chip->io_lock);
61
62 if (ret < 0)
63 return ret;
64 else
65 return (int)data;
66}
67EXPORT_SYMBOL(max8925_reg_read);
68
69int max8925_reg_write(struct i2c_client *i2c, int reg,
70 unsigned char data)
71{
72 struct max8925_chip *chip = i2c_get_clientdata(i2c);
73 int ret;
74
75 mutex_lock(&chip->io_lock);
76 ret = max8925_write_device(i2c, reg, 1, &data);
77 mutex_unlock(&chip->io_lock);
78
79 return ret;
80}
81EXPORT_SYMBOL(max8925_reg_write);
82
83int max8925_bulk_read(struct i2c_client *i2c, int reg,
84 int count, unsigned char *buf)
85{
86 struct max8925_chip *chip = i2c_get_clientdata(i2c);
87 int ret;
88
89 mutex_lock(&chip->io_lock);
90 ret = max8925_read_device(i2c, reg, count, buf);
91 mutex_unlock(&chip->io_lock);
92
93 return ret;
94}
95EXPORT_SYMBOL(max8925_bulk_read);
96
97int max8925_bulk_write(struct i2c_client *i2c, int reg,
98 int count, unsigned char *buf)
99{
100 struct max8925_chip *chip = i2c_get_clientdata(i2c);
101 int ret;
102
103 mutex_lock(&chip->io_lock);
104 ret = max8925_write_device(i2c, reg, count, buf);
105 mutex_unlock(&chip->io_lock);
106
107 return ret;
108}
109EXPORT_SYMBOL(max8925_bulk_write);
110
111int max8925_set_bits(struct i2c_client *i2c, int reg,
112 unsigned char mask, unsigned char data)
113{
114 struct max8925_chip *chip = i2c_get_clientdata(i2c);
115 unsigned char value;
116 int ret;
117
118 mutex_lock(&chip->io_lock);
119 ret = max8925_read_device(i2c, reg, 1, &value);
120 if (ret < 0)
121 goto out;
122 value &= ~mask;
123 value |= data;
124 ret = max8925_write_device(i2c, reg, 1, &value);
125out:
126 mutex_unlock(&chip->io_lock);
127 return ret;
128}
129EXPORT_SYMBOL(max8925_set_bits);
130
131
132static const struct i2c_device_id max8925_id_table[] = {
133 { "max8925", 0 },
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500134 { },
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100135};
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100136
Qing Xu4e405ae2013-02-04 23:40:42 +0800137static int max8925_dt_init(struct device_node *np, struct device *dev,
138 struct max8925_platform_data *pdata)
139{
140 int ret;
141
142 ret = of_property_read_u32(np, "maxim,tsc-irq", &pdata->tsc_irq);
143 if (ret) {
144 dev_err(dev, "Not found maxim,tsc-irq property\n");
145 return -EINVAL;
146 }
147 return 0;
148}
149
Bill Pembertonf791be42012-11-19 13:23:04 -0500150static int max8925_probe(struct i2c_client *client,
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100151 const struct i2c_device_id *id)
152{
Jingoo Han334a41c2013-07-30 17:10:05 +0900153 struct max8925_platform_data *pdata = dev_get_platdata(&client->dev);
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500154 static struct max8925_chip *chip;
Qing Xu4e405ae2013-02-04 23:40:42 +0800155 struct device_node *node = client->dev.of_node;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100156
Qing Xu4e405ae2013-02-04 23:40:42 +0800157 if (node && !pdata) {
158 /* parse DT to get platform data */
159 pdata = devm_kzalloc(&client->dev,
160 sizeof(struct max8925_platform_data),
161 GFP_KERNEL);
162 if (!pdata)
163 return -ENOMEM;
164
165 if (max8925_dt_init(node, &client->dev, pdata))
166 return -EINVAL;
167 } else if (!pdata) {
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100168 pr_info("%s: platform data is missing\n", __func__);
169 return -EINVAL;
170 }
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100171
Lee Jones02c7d842013-05-23 16:25:17 +0100172 chip = devm_kzalloc(&client->dev,
173 sizeof(struct max8925_chip), GFP_KERNEL);
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100174 if (chip == NULL)
175 return -ENOMEM;
176 chip->i2c = client;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100177 chip->dev = &client->dev;
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500178 i2c_set_clientdata(client, chip);
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100179 dev_set_drvdata(chip->dev, chip);
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500180 mutex_init(&chip->io_lock);
181
182 chip->rtc = i2c_new_dummy(chip->i2c->adapter, RTC_I2C_ADDR);
Krzysztof Kozlowski96cf3de2014-02-11 11:03:33 +0100183 if (!chip->rtc) {
184 dev_err(chip->dev, "Failed to allocate I2C device for RTC\n");
185 return -ENODEV;
186 }
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500187 i2c_set_clientdata(chip->rtc, chip);
188
189 chip->adc = i2c_new_dummy(chip->i2c->adapter, ADC_I2C_ADDR);
Krzysztof Kozlowski96cf3de2014-02-11 11:03:33 +0100190 if (!chip->adc) {
191 dev_err(chip->dev, "Failed to allocate I2C device for ADC\n");
192 i2c_unregister_device(chip->rtc);
193 return -ENODEV;
194 }
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500195 i2c_set_clientdata(chip->adc, chip);
196
Kevin Liuba74e802012-01-04 15:14:24 +0800197 device_init_wakeup(&client->dev, 1);
198
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100199 max8925_device_init(chip, pdata);
200
201 return 0;
202}
203
Bill Pemberton4740f732012-11-19 13:26:01 -0500204static int max8925_remove(struct i2c_client *client)
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100205{
206 struct max8925_chip *chip = i2c_get_clientdata(client);
207
208 max8925_device_exit(chip);
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500209 i2c_unregister_device(chip->adc);
210 i2c_unregister_device(chip->rtc);
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100211 return 0;
212}
213
Kevin Liuba74e802012-01-04 15:14:24 +0800214#ifdef CONFIG_PM_SLEEP
215static int max8925_suspend(struct device *dev)
216{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800217 struct i2c_client *client = to_i2c_client(dev);
Kevin Liuba74e802012-01-04 15:14:24 +0800218 struct max8925_chip *chip = i2c_get_clientdata(client);
219
220 if (device_may_wakeup(dev) && chip->wakeup_flag)
221 enable_irq_wake(chip->core_irq);
222 return 0;
223}
224
225static int max8925_resume(struct device *dev)
226{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800227 struct i2c_client *client = to_i2c_client(dev);
Kevin Liuba74e802012-01-04 15:14:24 +0800228 struct max8925_chip *chip = i2c_get_clientdata(client);
229
230 if (device_may_wakeup(dev) && chip->wakeup_flag)
231 disable_irq_wake(chip->core_irq);
232 return 0;
233}
234#endif
235
236static SIMPLE_DEV_PM_OPS(max8925_pm_ops, max8925_suspend, max8925_resume);
237
Qing Xu4e405ae2013-02-04 23:40:42 +0800238static const struct of_device_id max8925_dt_ids[] = {
239 { .compatible = "maxim,max8925", },
240 {},
241};
Qing Xu4e405ae2013-02-04 23:40:42 +0800242
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100243static struct i2c_driver max8925_driver = {
244 .driver = {
245 .name = "max8925",
Kevin Liuba74e802012-01-04 15:14:24 +0800246 .pm = &max8925_pm_ops,
Sachin Kamat52c2c6e2013-10-15 09:18:46 +0530247 .of_match_table = max8925_dt_ids,
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100248 },
249 .probe = max8925_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500250 .remove = max8925_remove,
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100251 .id_table = max8925_id_table,
252};
253
254static int __init max8925_i2c_init(void)
255{
256 int ret;
Lee Jones4ed8f712014-07-21 12:18:21 +0100257
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100258 ret = i2c_add_driver(&max8925_driver);
259 if (ret != 0)
260 pr_err("Failed to register MAX8925 I2C driver: %d\n", ret);
Lee Jones4ed8f712014-07-21 12:18:21 +0100261
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100262 return ret;
263}
264subsys_initcall(max8925_i2c_init);