aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/2mic/fm34_we395.c
blob: 677086d0f6da028d693177788f7124c5c167c0d1 (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
/* drivers/misc/2mic/fm34_we395.c - fm34_we395 voice processor driver
 *
 * Copyright (C) 2012 Samsung Corporation.
 *
 * 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.
 *
 */

#include <linux/delay.h>
#include <linux/freezer.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#include <linux/i2c/fm34_we395.h>
#include <linux/i2c/voice_processor.h>

#include "fm34_we395.h"

static struct class *fm34_class;
static struct device *fm34_dev;

struct fm34_data {
	struct fm34_platform_data	*pdata;
	struct device	*dev;
	struct i2c_client	*client;
	enum voice_processing_mode	curr_mode;
};

static void fm34_reset_parameter(struct fm34_data *fm34)
{
	gpio_set_value(fm34->pdata->gpio_rst, 0);
	usleep_range(10000, 10000);
	gpio_set_value(fm34->pdata->gpio_pwdn, 1);
	gpio_set_value(fm34->pdata->gpio_bp, 1);
	msleep(50);
	gpio_set_value(fm34->pdata->gpio_rst, 1);
	msleep(50);
}

static int fm34_set_mode(struct fm34_data *fm34,
					enum voice_processing_mode mode)
{
	int ret = 0;
	unsigned char *i2c_cmd;
	int size = 0;

	dev_dbg(fm34->dev, "%s : mode %d\n", __func__, mode);

	if (fm34->curr_mode == mode) {
		dev_dbg(fm34->dev, "skip %s\n", __func__);
		return ret;
	}

	switch (mode) {
	case VOICE_NS_BYPASS_MODE:
		usleep_range(20000, 20000);
		gpio_set_value(fm34->pdata->gpio_pwdn, 0);
		return ret;
	case VOICE_NS_HANDSET_MODE:
		i2c_cmd = handset_ns_cmd;
		size = sizeof(handset_ns_cmd);
		break;
	case VOICE_NS_LOUD_MODE:
		i2c_cmd = loud_ns_cmd;
		size = sizeof(loud_ns_cmd);
		break;
	case VOICE_NS_FTM_LOOPBACK_MODE:
		i2c_cmd = ftm_loopback_cmd;
		size = sizeof(ftm_loopback_cmd);
		break;
	default:
		break;
	}

	fm34_reset_parameter(fm34);

	if (size) {
		ret = i2c_master_send(fm34->client, i2c_cmd, size);
		if (ret < 0) {
			dev_err(fm34->dev, "i2c_master_send failed %d\n", ret);
			return ret;
		}
		fm34->curr_mode = mode;
	}

	return ret;
}

static ssize_t fm34_mode_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fm34_data *fm34 = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n", fm34->curr_mode);
}

static ssize_t fm34_mode_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	struct fm34_data *fm34 = dev_get_drvdata(dev);
	long mode;
	ssize_t status;

	status = strict_strtol(buf, 0, &mode);
	if (status == 0) {
		dev_info(fm34->dev, "%s mode = %ld\n", __func__, mode);
		fm34_set_mode(fm34, mode);
	} else
		dev_err(fm34->dev, "%s : operation is not valid\n", __func__);

	return status ? : size;
}

static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR | S_IWGRP,
		fm34_mode_show, fm34_mode_store);

static int fm34_init_parameter(struct fm34_data *fm34)
{
	int ret = 0;
	unsigned char *i2c_cmd;
	int size = 0;

	fm34_reset_parameter(fm34);

	i2c_cmd = bypass_cmd;
	size = sizeof(bypass_cmd);

	ret = i2c_master_send(fm34->client, i2c_cmd, size);
	if (ret < 0) {
		dev_err(fm34->dev, "i2c_master_send failed %d\n", ret);
		return ret;
	}

	fm34->curr_mode = VOICE_NS_BYPASS_MODE;

	usleep_range(20000, 20000);
	gpio_set_value(fm34->pdata->gpio_pwdn, 0);

	return ret;
}

static int __devinit fm34_probe(
		struct i2c_client *client, const struct i2c_device_id *id)
{
	struct fm34_data *fm34;
	struct fm34_platform_data *pdata;
	int ret = 0;

	fm34 = kzalloc(sizeof(*fm34), GFP_KERNEL);
	if (fm34 == NULL) {
		ret = -ENOMEM;
		goto err_kzalloc;
	}

	fm34->client = client;
	i2c_set_clientdata(client, fm34);

	fm34->dev = &client->dev;
	dev_set_drvdata(fm34->dev, fm34);

	fm34->pdata = client->dev.platform_data;
	pdata = fm34->pdata;

	fm34->curr_mode = -1;

	ret = gpio_request(pdata->gpio_rst, "FM34_RESET");
	if (ret < 0) {
		dev_err(fm34->dev, "error requesting reset gpio\n");
		goto err_gpio_reset;
	}
	gpio_direction_output(pdata->gpio_rst, 1);

	ret = gpio_request(pdata->gpio_pwdn, "FM34_PWDN");
	if (ret < 0) {
		dev_err(fm34->dev, "error requesting pwdn gpio\n");
		goto err_gpio_pwdn;
	}
	gpio_direction_output(pdata->gpio_pwdn, 1);

	ret = gpio_request(pdata->gpio_bp, "FM34_BYPASS");
	if (ret < 0) {
		dev_err(fm34->dev, "error requesting bypass gpio\n");
		goto err_gpio_bp;
	}
	gpio_direction_output(pdata->gpio_bp, 1);

	if (fm34->pdata->set_mclk != NULL)
		fm34->pdata->set_mclk(true, false);

	ret = fm34_init_parameter(fm34);
	if (ret < 0)
		dev_err(fm34->dev, "fm34_init_parameter failed %d\n", ret);

	fm34_class = class_create(THIS_MODULE, "voice_processor");
	if (IS_ERR(fm34_class)) {
		dev_err(fm34->dev, "Failed to create class(voice_processor) %ld\n",
				IS_ERR(fm34_class));
		goto err_class_create;
	}

	fm34_dev = device_create(fm34_class, NULL, 0, fm34, "2mic");
	if (IS_ERR(fm34_dev)) {
		dev_err(fm34->dev, "Failed to create device(2mic) %ld\n",
				IS_ERR(fm34_dev));
		goto err_device_create;
	}

	ret = device_create_file(fm34_dev, &dev_attr_mode);
	if (ret < 0)
		dev_err(fm34->dev, "Failed to create device file (%s) %d\n",
					dev_attr_mode.attr.name, ret);

	return 0;

err_gpio_bp:
	gpio_free(pdata->gpio_pwdn);
err_gpio_pwdn:
	gpio_free(pdata->gpio_rst);
err_gpio_reset:
	kfree(fm34);
err_kzalloc:
err_class_create:
err_device_create:
	return ret;
}

static int __devexit fm34_remove(struct i2c_client *client)
{
	struct fm34_data *fm34 = i2c_get_clientdata(client);
	i2c_set_clientdata(client, NULL);
	gpio_free(fm34->pdata->gpio_rst);
	gpio_free(fm34->pdata->gpio_pwdn);
	kfree(fm34);
	return 0;
}

static int fm34_suspend(struct device *dev)
{
	return 0;
}

static int fm34_resume(struct device *dev)
{
	return 0;
}

static const struct i2c_device_id fm34_id[] = {
	{ "fm34_we395", 0 },
	{ }
};

static const struct dev_pm_ops fm34_pm_ops = {
	.suspend = fm34_suspend,
	.resume = fm34_resume,
};

static struct i2c_driver fm34_driver = {
	.probe = fm34_probe,
	.remove = __devexit_p(fm34_remove),
	.id_table = fm34_id,
	.driver = {
		.name = "fm34_we395",
		.owner = THIS_MODULE,
		.pm = &fm34_pm_ops,
	},
};

static int __init fm34_init(void)
{
	pr_info("%s\n", __func__);

	return i2c_add_driver(&fm34_driver);
}

static void __exit fm34_exit(void)
{
	i2c_del_driver(&fm34_driver);
}

module_init(fm34_init);
module_exit(fm34_exit);

MODULE_DESCRIPTION("fm34 voice processor driver");
MODULE_LICENSE("GPL");