summaryrefstree
path: root/kernel/sound/soc/codecs/dbmdx/dbmdx-i2c-d4.c
blob: 56f139cc2d5171310f3a534b2afbabc0facb548f (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
/*
 * DSPG DBMD4 I2C interface driver
 *
 * Copyright (C) 2014 DSP Group
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */
/* #define DEBUG */
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/clk.h>
#include <linux/of.h>
#include <linux/firmware.h>

#include "dbmdx-interface.h"
#include "dbmdx-va-regmap.h"
#include "dbmdx-i2c.h"

#define DRIVER_VERSION			"1.0"


static const u8 clr_crc_cmd[] = {0x5A, 0x0F};



static int dbmd4_i2c_boot(const void *fw_data, size_t fw_size,
		struct dbmdx_private *p, const void *checksum,
		size_t chksum_len, int load_fw)
{
	int retry = RETRY_COUNT;
	int ret = 0;
	ssize_t send_bytes;
	struct dbmdx_i2c_private *i2c_p =
				(struct dbmdx_i2c_private *)p->chip->pdata;

	dev_dbg(i2c_p->dev, "%s\n", __func__);

	/* change to boot I2C address */
	i2c_p->client->addr = (unsigned short)(i2c_p->pdata->boot_addr);

	while (retry--) {

		if (p->active_fw == DBMDX_FW_PRE_BOOT) {

			/* reset DBMD4 chip */
			p->reset_sequence(p);

			/* delay before sending commands */
			if (p->clk_get_rate(p, DBMDX_CLK_MASTER) <= 32768)
				msleep(DBMDX_MSLEEP_I2C_D4_AFTER_RESET_32K);
			else
				usleep_range(DBMDX_USLEEP_I2C_D4_AFTER_RESET,
					DBMDX_USLEEP_I2C_D4_AFTER_RESET + 5000);

			/* send CRC clear command */
			ret = write_i2c_data(p, clr_crc_cmd,
				sizeof(clr_crc_cmd));
			if (ret != sizeof(clr_crc_cmd)) {
				dev_err(p->dev, "%s: failed to clear CRC\n",
					 __func__);
				continue;
			}
		} else {
			ret = send_i2c_cmd_va(p,
					DBMDX_VA_SWITCH_TO_BOOT,
					NULL);
			if (ret < 0) {
				dev_err(p->dev,
					"%s: failed to send 'Switch to BOOT' cmd\n",
					 __func__);
				continue;
			}
		}

		if (!load_fw)
			break;
		/* Sleep is needed here to ensure that chip is ready */
		msleep(DBMDX_MSLEEP_I2C_D4_AFTER_SBL);

		/* send firmware */
		send_bytes = write_i2c_data(p, fw_data, fw_size - 4);
		if (send_bytes != fw_size - 4) {
			dev_err(p->dev,
				"%s: -----------> load firmware error\n",
				__func__);
			continue;
		}

		msleep(DBMDX_MSLEEP_I2C_D4_BEFORE_FW_CHECKSUM);

		/* verify checksum */
		if (checksum) {
			ret = i2c_verify_boot_checksum(p, checksum, chksum_len);
			if (ret < 0) {
				dev_err(i2c_p->dev,
					"%s: could not verify checksum\n",
					__func__);
				continue;
			}
		}

		dev_info(p->dev, "%s: ---------> firmware loaded\n",
			__func__);
		break;
	}

	/* no retries left, failed to boot */
	if (retry < 0) {
		dev_err(p->dev, "%s: failed to load firmware\n", __func__);
		return -1;
	}

	/* send boot command */
	ret = send_i2c_cmd_boot(p, DBMDX_FIRMWARE_BOOT);
	if (ret < 0) {
		dev_err(p->dev, "%s: booting the firmware failed\n", __func__);
		return -1;
	}

	/* wait some time */
	usleep_range(DBMDX_USLEEP_I2C_D4_AFTER_BOOT,
		DBMDX_USLEEP_I2C_D4_AFTER_BOOT + 1000);

	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int dbmdx_i2c_suspend(struct device *dev)
{
	struct chip_interface *ci = i2c_get_clientdata(to_i2c_client(dev));
	struct dbmdx_i2c_private *i2c_p =
				(struct dbmdx_i2c_private *)ci->pdata;


	dev_dbg(dev, "%s\n", __func__);

	i2c_interface_suspend(i2c_p);

	return 0;
}

static int dbmdx_i2c_resume(struct device *dev)
{
	struct chip_interface *ci = i2c_get_clientdata(to_i2c_client(dev));
	struct dbmdx_i2c_private *i2c_p =
				(struct dbmdx_i2c_private *)ci->pdata;

	dev_dbg(dev, "%s\n", __func__);
	i2c_interface_resume(i2c_p);

	return 0;
}
#endif /* CONFIG_PM_SLEEP */

#ifdef CONFIG_PM_RUNTIME
static int dbmdx_i2c_runtime_suspend(struct device *dev)
{
	struct chip_interface *ci = i2c_get_clientdata(to_i2c_client(dev));
	struct dbmdx_i2c_private *i2c_p =
				(struct dbmdx_i2c_private *)ci->pdata;

	dev_dbg(dev, "%s\n", __func__);

	i2c_interface_suspend(i2c_p);

	return 0;
}

static int dbmdx_i2c_runtime_resume(struct device *dev)
{
	struct chip_interface *ci = i2c_get_clientdata(to_i2c_client(dev));
	struct dbmdx_i2c_private *i2c_p =
				(struct dbmdx_i2c_private *)ci->pdata;

	dev_dbg(dev, "%s\n", __func__);
	i2c_interface_resume(i2c_p);

	return 0;
}

#endif /* CONFIG_PM_RUNTIME */

static const struct dev_pm_ops dbmdx_i2c_pm = {
	SET_SYSTEM_SLEEP_PM_OPS(dbmdx_i2c_suspend, dbmdx_i2c_resume)
	SET_RUNTIME_PM_OPS(dbmdx_i2c_runtime_suspend,
			   dbmdx_i2c_runtime_resume, NULL)
};



static int dbmd4_i2c_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	int rc;
	struct dbmdx_i2c_private *p;
	struct chip_interface *ci;

	rc = i2c_common_probe(client, id);

	if (rc < 0)
		return rc;

	ci = i2c_get_clientdata(client);
	p = (struct dbmdx_i2c_private *)ci->pdata;

	/* fill in chip interface functions */
	p->chip.boot = dbmd4_i2c_boot;

	return rc;
}


static const struct of_device_id dbmd_4_6_i2c_of_match[] = {
	{ .compatible = "dspg,dbmd4-i2c", },
	{ .compatible = "dspg,dbmd6-i2c", },
	{},
};
MODULE_DEVICE_TABLE(of, dbmd_4_6_i2c_of_match);

static const struct i2c_device_id dbmd_4_6_i2c_id[] = {
	{ "dbmdx-i2c", 0 },
	{ "dbmd4-i2c", 0 },
	{ "dbmd6-i2c", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, dbmd_4_6_i2c_id);

static struct i2c_driver dbmd_4_6_i2c_driver = {
	.driver = {
		.name = "dbmd_4_6-i2c",
		.owner = THIS_MODULE,
		.of_match_table = dbmd_4_6_i2c_of_match,
		.pm = &dbmdx_i2c_pm,
	},
	.probe =    dbmd4_i2c_probe,
	.remove =   i2c_common_remove,
	.id_table = dbmd_4_6_i2c_id,
};

static int __init dbmd_4_6_modinit(void)
{
	return i2c_add_driver(&dbmd_4_6_i2c_driver);
}
module_init(dbmd_4_6_modinit);

static void __exit dbmd_4_6_exit(void)
{
	i2c_del_driver(&dbmd_4_6_i2c_driver);
}
module_exit(dbmd_4_6_exit);

MODULE_DESCRIPTION("DSPG DBMD4/DBMD6 I2C interface driver");
MODULE_LICENSE("GPL");