summaryrefstree
path: root/kernel/drivers/mfd/s2mu003_core.c
blob: 8e9e4707354a93cbb3ce6ba9fa9023172aa1d075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* drivers/mfd/s2mu003_core.c
 * S2MU003 Multifunction Device Driver
 * Charger / Buck / LDOs / FlashLED
 *
 * Copyright (C) 2014 Samsung Technology Corp.
 * Author: Patrick Chang <patrick_chang@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 */

#include <linux/errno.h>
#include <linux/version.h>
#include <linux/device.h>
#include <linux/pm.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/irqdomain.h>
#include <linux/of.h>
#include <linux/of_gpio.h>

#include <linux/mfd/samsung/s2mu003.h>
#include <linux/mfd/samsung/s2mu003_irq.h>
#include <linux/mfd/core.h>
#ifdef CONFIG_CHARGER_S2MU003
#include <linux/battery/charger/s2mu003_charger.h>
#endif

#if defined(CONFIG_MFD_S2MU003_USE_DT)
#define S2MU003_USE_NEW_MFD_DT_API
#endif

#ifdef CONFIG_CHARGER_S2MU003
const static struct resource s2mu003_charger_res[] = {
	S2MU003_DECLARE_IRQ(S2MU003_EOC_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CINIR_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_BATP_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_BATLV_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_TOPOFF_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CINOVP_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CHGTSD_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CHGVINVR_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CHGTR_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_TMROUT_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_RECHG_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CHGTERM_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_BATOVP_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CHGVIN_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CIN2VAT_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_CHGSTS_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_OTGILIM_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_BSTINLV_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_BSTILIM_IRQ),
	S2MU003_DECLARE_IRQ(S2MU003_VMIDOVP_IRQ),
};

static struct mfd_cell s2mu003_charger_devs[] = {
	{
		.name		= "s2mu003-charger",
		.num_resources	= ARRAY_SIZE(s2mu003_charger_res),
		.id		= -1,
		.resources = s2mu003_charger_res,
#ifdef CONFIG_OF
		.of_compatible = "samsung,s2mu003-charger"
#endif
	},
};
#endif /*CONFIG_CHARGER_S2MU003*/

#ifdef CONFIG_LEDS_S2MU003
static struct mfd_cell s2mu003_fled_devs[] = {
	{
		.name		= "s2mu003-leds",
#ifdef CONFIG_OF
	.of_compatible = "samsung,s2mu003-leds"
#endif
	},
};
#endif

#ifdef CONFIG_REGULATOR_S2MU003
static struct mfd_cell s2mu003_regulator_devs[] = {
	{ .name = "s2mu003-regulator", },
};
#endif /* CONFIG_REGULATOR_S2MU003 */

const static uint8_t s2mu003_chg_group1_default[] = { 0x40, 0x58 };
const static uint8_t s2mu003_chg_group2_default[] = {0x41, 0xAB, 0x35};

static inline int s2mu003_read_device(struct i2c_client *i2c,
				int reg, int bytes, void *dest)
{
	int ret;
	if (bytes > 1) {
		ret = i2c_smbus_read_i2c_block_data(i2c, reg, bytes, dest);
	} else {
		ret = i2c_smbus_read_byte_data(i2c, reg);
		if (ret < 0)
			return ret;
		*(u8 *)dest = (u8)ret;
	}
	return ret;
}

static inline int s2mu003_write_device(struct i2c_client *i2c,
				int reg, int bytes, void *src)
{
	int ret;
	uint8_t *data;
	if (bytes > 1)
		ret = i2c_smbus_write_i2c_block_data(i2c, reg, bytes, src);
	else {
		data = src;
		ret = i2c_smbus_write_byte_data(i2c, reg, *data);
	}
	return ret;
}

int s2mu003_block_read_device(struct i2c_client *i2c,
			int reg, int bytes, void *dest)
{
	struct s2mu003_mfd_chip *chip = i2c_get_clientdata(i2c);
	int ret;
	mutex_lock(&chip->io_lock);
	ret = s2mu003_read_device(i2c, reg, bytes, dest);
	mutex_unlock(&chip->io_lock);
	return ret;
}
EXPORT_SYMBOL(s2mu003_block_read_device);

int s2mu003_block_write_device(struct i2c_client *i2c,
		int reg, int bytes, void *src)
{
	struct s2mu003_mfd_chip *chip = i2c_get_clientdata(i2c);
	int ret;

	if (!chip)
		dev_err(&i2c->dev, "Failed to get the clientdata\n");

	mutex_lock(&chip->io_lock);
	ret = s2mu003_write_device(i2c, reg, bytes, src);
	mutex_unlock(&chip->io_lock);
	return ret;
}
EXPORT_SYMBOL(s2mu003_block_write_device);

int s2mu003_reg_read(struct i2c_client *i2c, int reg)
{
	struct s2mu003_mfd_chip *chip = i2c_get_clientdata(i2c);
	u8 data = 0;
	int ret;

	if (!chip)
		dev_err(&i2c->dev, "Failed to get the clientdata\n");

	mutex_lock(&chip->io_lock);
	ret = s2mu003_read_device(i2c, reg, 1, &data);
	mutex_unlock(&chip->io_lock);

	if (ret < 0)
		return ret;
	else
		return (int)data;
}
EXPORT_SYMBOL(s2mu003_reg_read);

int s2mu003_reg_write(struct i2c_client *i2c, int reg,
		u8 data)
{
	struct s2mu003_mfd_chip *chip = i2c_get_clientdata(i2c);
	int ret;

	if (!chip)
		dev_err(&i2c->dev, "Failed to get the clientdata\n");

	mutex_lock(&chip->io_lock);
	ret = i2c_smbus_write_byte_data(i2c, reg, data);
	mutex_unlock(&chip->io_lock);

	return ret;
}
EXPORT_SYMBOL(s2mu003_reg_write);

int s2mu003_assign_bits(struct i2c_client *i2c, int reg,
		u8 mask, u8 data)
{
	struct s2mu003_mfd_chip *chip = i2c_get_clientdata(i2c);
	u8 value;
	int ret;

	if (!chip)
		dev_err(&i2c->dev, "Failed to get the clientdata\n");

	mutex_lock(&chip->io_lock);
	ret = s2mu003_read_device(i2c, reg, 1, &value);

	if (ret < 0)
		goto out;

	value &= ~mask;
	value |= data;
	ret = i2c_smbus_write_byte_data(i2c, reg, value);

out:
	mutex_unlock(&chip->io_lock);
	return ret;
}
EXPORT_SYMBOL(s2mu003_assign_bits);

int s2mu003_set_bits(struct i2c_client *i2c, int reg,
		u8 mask)
{
	return s2mu003_assign_bits(i2c, reg, mask, mask);
}
EXPORT_SYMBOL(s2mu003_set_bits);

int s2mu003_clr_bits(struct i2c_client *i2c, int reg,
		u8 mask)
{
	return s2mu003_assign_bits(i2c, reg, mask, 0);
}
EXPORT_SYMBOL(s2mu003_clr_bits);

static int s2mu003mfd_parse_dt(struct device *dev,
		s2mu003_mfd_platform_data_t *pdata)
{
	int ret;
	struct device_node *np = dev->of_node;

	if (np == NULL) {
		pr_err("%s np NULL\n", __func__);
		return -EINVAL;
	}

	ret = pdata->irq_gpio = of_get_named_gpio(np, "s2mu003,irq-gpio", 0);
	if (ret < 0) {
		dev_err(dev, "%s : can't get irq-gpio\r\n", __func__);
		return ret;
	}

	pdata->irq_base = -1;
	return 0;
}

static int s2mu003_mfd_probe(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
{
	int ret = 0;
	u8 data = 0;
	struct device_node *of_node = i2c->dev.of_node;
	s2mu003_mfd_chip_t *chip;
	s2mu003_mfd_platform_data_t *pdata = i2c->dev.platform_data;

	pr_info("%s : S2MU003 MFD Driver start probe\n", __func__);
	if (of_node) {
		pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
		if (!pdata) {
			dev_err(&i2c->dev, "Failed to allocate memory\n");
			ret = -ENOMEM;
			goto err_dt_nomem;
		}
		ret = s2mu003mfd_parse_dt(&i2c->dev, pdata);
		if (ret < 0)
			goto err_parse_dt;
	} else {
		pdata = i2c->dev.platform_data;
	}

	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
	if (chip ==  NULL) {
		dev_err(&i2c->dev, "Memory is not enough.\n");
		ret = -ENOMEM;
		goto err_mfd_nomem;
	}
	chip->dev = &i2c->dev;

	ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
			I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_I2C_BLOCK);
	if (!ret) {
		ret = i2c_get_functionality(i2c->adapter);
		dev_err(chip->dev, "I2C functionality is not supported.\n");
		ret = -ENOSYS;
		goto err_i2cfunc_not_support;
	}

	chip->i2c_client = i2c;
	chip->pdata = pdata;

	/* if board-init had already assigned irq_base (>=0) ,
	no need to allocate it;
	assign -1 to let this driver allocate resource by itself*/
	if (pdata->irq_base < 0)
		pdata->irq_base = irq_alloc_descs(-1, 0, S2MU003_IRQS_NR, 0);
	if (pdata->irq_base < 0) {
		pr_err("%s:%s irq_alloc_descs Fail! ret(%d)\n",
				"s2mu003-mfd", __func__, pdata->irq_base);
		ret = -EINVAL;
		goto irq_base_err;
	} else {
		chip->irq_base = pdata->irq_base;
		pr_info("%s:%s irq_base = %d\n",
				"s2mu003-mfd", __func__, chip->irq_base);
	irq_domain_add_legacy(of_node, S2MU003_IRQS_NR, chip->irq_base, 0,
				&irq_domain_simple_ops, NULL);
	}

	i2c_set_clientdata(i2c, chip);
	mutex_init(&chip->io_lock);

	wake_lock_init(&(chip->irq_wake_lock), WAKE_LOCK_SUSPEND,
			"s2mu003mfd_wakelock");

	/* To disable MRST function should be
	finished before set any reg init-value*/
	data = s2mu003_reg_read(i2c, 0x47);
	pr_info("%s : Manual Reset Data = 0x%x", __func__, data);
	s2mu003_clr_bits(i2c, 0x47, 1<<3); /*Disable Manual Reset*/

	ret = s2mu003_init_irq(chip);

	if (ret < 0) {
		dev_err(chip->dev,
				"Error : can't initialize S2MU003 MFD irq\n");
		goto err_init_irq;
	}

#ifdef CONFIG_REGULATOR_S2MU003
	ret = mfd_add_devices(chip->dev, 0, &s2mu003_regulator_devs[0],
			ARRAY_SIZE(s2mu003_regulator_devs),
			NULL, chip->irq_base, NULL);
	if (ret < 0) {
		dev_err(chip->dev,
				"Error : can't add regulator\n");
		goto err_add_regulator_devs;
	}
#endif /*CONFIG_REGULATOR_S2MU003*/

#ifdef CONFIG_LEDS_S2MU003
	ret = mfd_add_devices(chip->dev, 0, &s2mu003_fled_devs[0],
			ARRAY_SIZE(s2mu003_fled_devs),
			NULL, chip->irq_base, NULL);
	if (ret < 0) {
		dev_err(chip->dev, "Failed : add FlashLED devices");
		goto err_add_fled_devs;
	}
#endif /*CONFIG_LEDS_S2MU003*/


#ifdef CONFIG_CHARGER_S2MU003
	ret = mfd_add_devices(chip->dev, 0, &s2mu003_charger_devs[0],
			ARRAY_SIZE(s2mu003_charger_devs),
			NULL, chip->irq_base, NULL);
	if (ret < 0) {
		dev_err(chip->dev, "Failed : add charger devices\n");
		goto err_add_chg_devs;
	}
#endif /*CONFIG_CHARGER_S2MU003*/

	device_init_wakeup(chip->dev, 1);
	enable_irq_wake(chip->irq);

	pr_info("%s : S2MU003 MFD Driver Fin probe\n", __func__);
	return ret;

#ifdef CONFIG_CHARGER_S2MU003
err_add_chg_devs:
#endif /*CONFIG_CHARGER_S2MU003*/

#ifdef CONFIG_LEDS_S2MU003
err_add_fled_devs:
#endif /*CONFIG_LEDS_S2MU003*/
	mfd_remove_devices(chip->dev);
#ifdef CONFIG_REGULATOR_S2MU003
err_add_regulator_devs:
#endif /*CONFIG_REGULATOR_S2MU003*/
err_init_irq:
	wake_lock_destroy(&(chip->irq_wake_lock));
	mutex_destroy(&chip->io_lock);
	kfree(chip);
irq_base_err:
err_mfd_nomem:
err_i2cfunc_not_support:
err_parse_dt:
err_dt_nomem:
	return ret;
}

static int s2mu003_mfd_remove(struct i2c_client *i2c)
{
	s2mu003_mfd_chip_t *chip = i2c_get_clientdata(i2c);

	pr_info("%s : S2MU003 MFD Driver remove\n", __func__);

	mfd_remove_devices(chip->dev);
	wake_lock_destroy(&(chip->irq_wake_lock));
	mutex_destroy(&chip->io_lock);
	kfree(chip);

	return 0;
}

#ifdef CONFIG_PM
/*check here cause error
  type define must de changed....
  use struct device *dev for function attribute
 */
int s2mu003_mfd_suspend(struct device *dev)
{

	struct i2c_client *i2c =
		container_of(dev, struct i2c_client, dev);

	s2mu003_mfd_chip_t *chip = i2c_get_clientdata(i2c);
	BUG_ON(chip == NULL);

	disable_irq(chip->irq);

	return 0;
}

int s2mu003_mfd_resume(struct device *dev)
{
	struct i2c_client *i2c =
		container_of(dev, struct i2c_client, dev);
	s2mu003_mfd_chip_t *chip = i2c_get_clientdata(i2c);
	BUG_ON(chip == NULL);

	pr_info("%s: S2MU003 Resume\n", __func__);

	enable_irq(chip->irq);

	return 0;
}
#endif /* CONFIG_PM */

static void s2mu003_mfd_shutdown(struct i2c_client *client)
{
}

static const struct i2c_device_id s2mu003_mfd_id_table[] = {
	{ "s2mu003-mfd", 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, s2mu003_id_table);


#ifdef CONFIG_PM
const struct dev_pm_ops s2mu003_pm = {
	.suspend = s2mu003_mfd_suspend,
	.resume = s2mu003_mfd_resume,

};
#endif

#ifdef CONFIG_OF
static struct of_device_id s2mu003_match_table[] = {
	{ .compatible = "samsung,s2mu003mfd",},
	{},
};
#else
#define s2mu003_match_table NULL
#endif

static struct i2c_driver s2mu003_mfd_driver = {
	.driver	= {
		.name	= "s2mu003-mfd",
		.owner	= THIS_MODULE,
		.of_match_table = s2mu003_match_table,
#ifdef CONFIG_PM
		.pm		= &s2mu003_pm,
#endif
	},
	.shutdown	= s2mu003_mfd_shutdown,
	.probe		= s2mu003_mfd_probe,
	.remove		= s2mu003_mfd_remove,
	.id_table	= s2mu003_mfd_id_table,
};

static int __init s2mu003_mfd_i2c_init(void)
{
	int ret;

	ret = i2c_add_driver(&s2mu003_mfd_driver);
	if (ret != 0)
		pr_info("%s : Failed to register S2MU003 MFD I2C driver\n",
				__func__);

	return ret;
}
subsys_initcall(s2mu003_mfd_i2c_init);

static void __exit s2mu003_mfd_i2c_exit(void)
{
	i2c_del_driver(&s2mu003_mfd_driver);
}
module_exit(s2mu003_mfd_i2c_exit);

MODULE_DESCRIPTION("Richtek S2MU003 MFD I2C Driver");
MODULE_AUTHOR("Patrick Chang <patrick_chang@samsung.com>");
MODULE_VERSION(S2MU003_DRV_VER);
MODULE_LICENSE("GPL");