blob: 114e905bef25ae1c46a2c4aede9fa4848fc6e22f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haojian Zhuangd50f8f32010-01-08 12:29:23 +01002/*
3 * I2C driver for Maxim MAX8925
4 *
5 * Copyright (C) 2009 Marvell International Ltd.
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
Haojian Zhuangd50f8f32010-01-08 12:29:23 +01007 */
8#include <linux/kernel.h>
Paul Gortmaker9eb5f9b2016-06-02 20:39:46 -04009#include <linux/init.h>
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010010#include <linux/platform_device.h>
11#include <linux/i2c.h>
12#include <linux/mfd/max8925.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010014
Haojian Zhuangb13c0df2010-01-25 06:26:34 -050015#define RTC_I2C_ADDR 0x68
16#define ADC_I2C_ADDR 0x47
17
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010018static inline int max8925_read_device(struct i2c_client *i2c,
19 int reg, int bytes, void *dest)
20{
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010021 int ret;
22
Haojian Zhuangb13c0df2010-01-25 06:26:34 -050023 if (bytes > 1)
24 ret = i2c_smbus_read_i2c_block_data(i2c, reg, bytes, dest);
25 else {
26 ret = i2c_smbus_read_byte_data(i2c, reg);
27 if (ret < 0)
28 return ret;
29 *(unsigned char *)dest = (unsigned char)ret;
30 }
31 return ret;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010032}
33
34static inline int max8925_write_device(struct i2c_client *i2c,
35 int reg, int bytes, void *src)
36{
Lee Jones87bd1c92014-08-28 11:01:15 +010037 unsigned char buf[9];
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010038 int ret;
39
40 buf[0] = (unsigned char)reg;
41 memcpy(&buf[1], src, bytes);
42
43 ret = i2c_master_send(i2c, buf, bytes + 1);
44 if (ret < 0)
45 return ret;
46 return 0;
47}
48
49int max8925_reg_read(struct i2c_client *i2c, int reg)
50{
51 struct max8925_chip *chip = i2c_get_clientdata(i2c);
Haojian Zhuangb13c0df2010-01-25 06:26:34 -050052 unsigned char data = 0;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +010053 int ret;
54
55 mutex_lock(&chip->io_lock);
56 ret = max8925_read_device(i2c, reg, 1, &data);
57 mutex_unlock(&chip->io_lock);
58
59 if (ret < 0)
60 return ret;
61 else
62 return (int)data;
63}
64EXPORT_SYMBOL(max8925_reg_read);
65
66int max8925_reg_write(struct i2c_client *i2c, int reg,
67 unsigned char data)
68{
69 struct max8925_chip *chip = i2c_get_clientdata(i2c);
70 int ret;
71
72 mutex_lock(&chip->io_lock);
73 ret = max8925_write_device(i2c, reg, 1, &data);
74 mutex_unlock(&chip->io_lock);
75
76 return ret;
77}
78EXPORT_SYMBOL(max8925_reg_write);
79
80int max8925_bulk_read(struct i2c_client *i2c, int reg,
81 int count, unsigned char *buf)
82{
83 struct max8925_chip *chip = i2c_get_clientdata(i2c);
84 int ret;
85
86 mutex_lock(&chip->io_lock);
87 ret = max8925_read_device(i2c, reg, count, buf);
88 mutex_unlock(&chip->io_lock);
89
90 return ret;
91}
92EXPORT_SYMBOL(max8925_bulk_read);
93
94int max8925_bulk_write(struct i2c_client *i2c, int reg,
95 int count, unsigned char *buf)
96{
97 struct max8925_chip *chip = i2c_get_clientdata(i2c);
98 int ret;
99
100 mutex_lock(&chip->io_lock);
101 ret = max8925_write_device(i2c, reg, count, buf);
102 mutex_unlock(&chip->io_lock);
103
104 return ret;
105}
106EXPORT_SYMBOL(max8925_bulk_write);
107
108int max8925_set_bits(struct i2c_client *i2c, int reg,
109 unsigned char mask, unsigned char data)
110{
111 struct max8925_chip *chip = i2c_get_clientdata(i2c);
112 unsigned char value;
113 int ret;
114
115 mutex_lock(&chip->io_lock);
116 ret = max8925_read_device(i2c, reg, 1, &value);
117 if (ret < 0)
118 goto out;
119 value &= ~mask;
120 value |= data;
121 ret = max8925_write_device(i2c, reg, 1, &value);
122out:
123 mutex_unlock(&chip->io_lock);
124 return ret;
125}
126EXPORT_SYMBOL(max8925_set_bits);
127
128
129static const struct i2c_device_id max8925_id_table[] = {
130 { "max8925", 0 },
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500131 { },
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100132};
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100133
Qing Xu4e405ae2013-02-04 23:40:42 +0800134static int max8925_dt_init(struct device_node *np, struct device *dev,
135 struct max8925_platform_data *pdata)
136{
137 int ret;
138
139 ret = of_property_read_u32(np, "maxim,tsc-irq", &pdata->tsc_irq);
140 if (ret) {
141 dev_err(dev, "Not found maxim,tsc-irq property\n");
142 return -EINVAL;
143 }
144 return 0;
145}
146
Bill Pembertonf791be42012-11-19 13:23:04 -0500147static int max8925_probe(struct i2c_client *client,
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100148 const struct i2c_device_id *id)
149{
Jingoo Han334a41ce2013-07-30 17:10:05 +0900150 struct max8925_platform_data *pdata = dev_get_platdata(&client->dev);
Julia Lawall09530752017-05-04 22:10:54 +0200151 struct max8925_chip *chip;
Qing Xu4e405ae2013-02-04 23:40:42 +0800152 struct device_node *node = client->dev.of_node;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100153
Qing Xu4e405ae2013-02-04 23:40:42 +0800154 if (node && !pdata) {
155 /* parse DT to get platform data */
156 pdata = devm_kzalloc(&client->dev,
157 sizeof(struct max8925_platform_data),
158 GFP_KERNEL);
159 if (!pdata)
160 return -ENOMEM;
161
162 if (max8925_dt_init(node, &client->dev, pdata))
163 return -EINVAL;
164 } else if (!pdata) {
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100165 pr_info("%s: platform data is missing\n", __func__);
166 return -EINVAL;
167 }
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100168
Lee Jones02c7d842013-05-23 16:25:17 +0100169 chip = devm_kzalloc(&client->dev,
170 sizeof(struct max8925_chip), GFP_KERNEL);
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100171 if (chip == NULL)
172 return -ENOMEM;
173 chip->i2c = client;
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100174 chip->dev = &client->dev;
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500175 i2c_set_clientdata(client, chip);
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100176 dev_set_drvdata(chip->dev, chip);
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500177 mutex_init(&chip->io_lock);
178
Wolfram Sangddbf6ff2019-07-22 19:26:17 +0200179 chip->rtc = i2c_new_dummy_device(chip->i2c->adapter, RTC_I2C_ADDR);
180 if (IS_ERR(chip->rtc)) {
Krzysztof Kozlowski96cf3de2014-02-11 11:03:33 +0100181 dev_err(chip->dev, "Failed to allocate I2C device for RTC\n");
Wolfram Sangddbf6ff2019-07-22 19:26:17 +0200182 return PTR_ERR(chip->rtc);
Krzysztof Kozlowski96cf3de2014-02-11 11:03:33 +0100183 }
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500184 i2c_set_clientdata(chip->rtc, chip);
185
Wolfram Sangddbf6ff2019-07-22 19:26:17 +0200186 chip->adc = i2c_new_dummy_device(chip->i2c->adapter, ADC_I2C_ADDR);
187 if (IS_ERR(chip->adc)) {
Krzysztof Kozlowski96cf3de2014-02-11 11:03:33 +0100188 dev_err(chip->dev, "Failed to allocate I2C device for ADC\n");
189 i2c_unregister_device(chip->rtc);
Wolfram Sangddbf6ff2019-07-22 19:26:17 +0200190 return PTR_ERR(chip->adc);
Krzysztof Kozlowski96cf3de2014-02-11 11:03:33 +0100191 }
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500192 i2c_set_clientdata(chip->adc, chip);
193
Kevin Liuba74e802012-01-04 15:14:24 +0800194 device_init_wakeup(&client->dev, 1);
195
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100196 max8925_device_init(chip, pdata);
197
198 return 0;
199}
200
Bill Pemberton4740f732012-11-19 13:26:01 -0500201static int max8925_remove(struct i2c_client *client)
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100202{
203 struct max8925_chip *chip = i2c_get_clientdata(client);
204
205 max8925_device_exit(chip);
Haojian Zhuangb13c0df2010-01-25 06:26:34 -0500206 i2c_unregister_device(chip->adc);
207 i2c_unregister_device(chip->rtc);
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100208 return 0;
209}
210
Kevin Liuba74e802012-01-04 15:14:24 +0800211#ifdef CONFIG_PM_SLEEP
212static int max8925_suspend(struct device *dev)
213{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800214 struct i2c_client *client = to_i2c_client(dev);
Kevin Liuba74e802012-01-04 15:14:24 +0800215 struct max8925_chip *chip = i2c_get_clientdata(client);
216
217 if (device_may_wakeup(dev) && chip->wakeup_flag)
218 enable_irq_wake(chip->core_irq);
219 return 0;
220}
221
222static int max8925_resume(struct device *dev)
223{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800224 struct i2c_client *client = to_i2c_client(dev);
Kevin Liuba74e802012-01-04 15:14:24 +0800225 struct max8925_chip *chip = i2c_get_clientdata(client);
226
227 if (device_may_wakeup(dev) && chip->wakeup_flag)
228 disable_irq_wake(chip->core_irq);
229 return 0;
230}
231#endif
232
233static SIMPLE_DEV_PM_OPS(max8925_pm_ops, max8925_suspend, max8925_resume);
234
Qing Xu4e405ae2013-02-04 23:40:42 +0800235static const struct of_device_id max8925_dt_ids[] = {
236 { .compatible = "maxim,max8925", },
237 {},
238};
Qing Xu4e405ae2013-02-04 23:40:42 +0800239
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100240static struct i2c_driver max8925_driver = {
241 .driver = {
242 .name = "max8925",
Kevin Liuba74e802012-01-04 15:14:24 +0800243 .pm = &max8925_pm_ops,
Sachin Kamat52c2c6e2013-10-15 09:18:46 +0530244 .of_match_table = max8925_dt_ids,
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100245 },
246 .probe = max8925_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500247 .remove = max8925_remove,
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100248 .id_table = max8925_id_table,
249};
250
251static int __init max8925_i2c_init(void)
252{
253 int ret;
Lee Jones4ed8f712014-07-21 12:18:21 +0100254
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100255 ret = i2c_add_driver(&max8925_driver);
256 if (ret != 0)
257 pr_err("Failed to register MAX8925 I2C driver: %d\n", ret);
Lee Jones4ed8f712014-07-21 12:18:21 +0100258
Haojian Zhuangd50f8f32010-01-08 12:29:23 +0100259 return ret;
260}
261subsys_initcall(max8925_i2c_init);