summaryrefstree
path: root/kernel/drivers/muic/universal/muic_i2c.c
blob: 8a35baa6bc11bd359617b0e5b13c9028b3c6a52c (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
/*
 * muic_i2c.c - SM5703 micro USB switch device driver
 *
 * Copyright (C) 2014 Samsung Electronics
 * Thomas Ryu <smilesr.ryu@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/host_notify.h>

#include <linux/muic/muic.h>

#if defined(CONFIG_MUIC_NOTIFIER)
#include <linux/muic/muic_notifier.h>
#endif /* CONFIG_MUIC_NOTIFIER */

#if defined (CONFIG_OF)
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#endif /* CONFIG_OF */

#include "muic-internal.h"
#include "muic_debug.h"

int muic_i2c_read_byte(const struct i2c_client *client, u8 command)
{
	int ret;
	int retry = 0;

	ret = i2c_smbus_read_byte_data(client, command);

	while(ret < 0){
		if(retry > 3)
		{
			pr_err("%s: retry count > 3 : failed !!\n", __func__);
			break;
		}

		pr_err("%s:i2c err on reading reg(0x%x), retrying ...\n",
					__func__, command);
		msleep(100);
		ret = i2c_smbus_read_byte_data(client, command);
		retry ++;
	}
#ifdef DEBUG_MUIC
	muic_reg_log(command, ret, retry << 1| READ);
#endif
	return ret;
}

int muic_i2c_write_byte(const struct i2c_client *client,
			u8 command, u8 value)
{
	int ret;
	int retry = 0;

	ret = i2c_smbus_write_byte_data(client, command, value);

	while(ret < 0) {
		if(retry > 3)
		{
			pr_err("%s: retry count > 3 : failed !!\n", __func__);
			break;
		}

		pr_err("%s:i2c err on writing reg(0x%x), retrying ...\n",
					__func__, command);
		msleep(100);
		ret = i2c_smbus_write_byte_data(client, command, value);
		retry ++;
	}
#ifdef DEBUG_MUIC
	muic_reg_log(command, value, retry << 1| WRITE);
#endif
	return ret;
}

int muic_i2c_guaranteed_wbyte(const struct i2c_client *client,
			u8 command, u8 value)
{
	int ret;
	int retry = 0;
	int written;

	ret = muic_i2c_write_byte(client, command, value);
	written = muic_i2c_read_byte(client, command);

	while(written != value){
		pr_err("%s:reg(0x%x): written(0x%x) != value(0x%x)...\n",
					__func__, command, written, value);
		if(retry > 5)
		{
			pr_err("%s: retry count > 5 : failed !!\n", __func__);
			break;
		}
		msleep(100);
		retry ++;
		ret = muic_i2c_write_byte(client, command, value);
		written = muic_i2c_read_byte(client, command);
	}
	return ret;
}