summaryrefslogtreecommitdiff
path: root/drivers/misc/c55_ctrl.c
blob: 54ffc6e5b2bfa27535f8d67950f3321cd182619f (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
/*
 * Copyright (C) 2013 Motorola Mobility LLC
 *
 * 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/err.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/m4sensorhub.h>
#include <linux/m4sensorhub/MemMapUserSettings.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/wakelock.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/clk.h>

enum {
	C55_OFF,
	C55_ON,
	C55_MODE_MAX
};

struct c55_ctrl_data {
	int c55_ap_int_gpio;
	int ap_c55_int_gpio;
	int c55_ap_int_enabled;
	struct wake_lock wake_lock;
	struct regulator *reg_vddc;
	struct regulator *reg_vddldo;
	struct pinctrl *pctrl;
	struct pinctrl_state *states[C55_MODE_MAX];
	int c55_mode;
	struct mutex ctrl_mutex;	/* mutex to handle critical area */
	int fw_verified;
};

const char *c55_pin_state_labels[C55_MODE_MAX] = {
	"off",
	"on"
};

#define NUM_GPIOS 2

const char *gpio_labels[NUM_GPIOS] = {
	"gpio_ap_int",
	"gpio_c55_int"
};

static irqreturn_t c55_ctrl_isr(int irq, void *data)
{
	struct c55_ctrl_data *cdata = data;

	pr_debug("%s: value=%d\n", __func__,
		 gpio_get_value(cdata->c55_ap_int_gpio));

	/* Interrupt is active low */
	if (gpio_get_value(cdata->c55_ap_int_gpio) == 0)
		wake_lock(&cdata->wake_lock);
	else
		wake_unlock(&cdata->wake_lock);

	return IRQ_HANDLED;
}

static void c55_ctrl_int_setup(struct c55_ctrl_data *cdata, int gpio)
{
	int ret;
	int irq = __gpio_to_irq(gpio);
	unsigned int flags = 0;

	if (cdata->c55_ap_int_gpio >= 0) {
		/* Interrupt already registered */
		return;
	}

	/* Interrupt is shared with user space */
	flags |= IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
	flags |= IRQF_SHARED;

	cdata->c55_ap_int_enabled = 1;
	ret = request_threaded_irq(irq, c55_ctrl_isr, NULL,
				   flags, "c55_ctrl", cdata);
	if (ret) {
		pr_err("%s: IRQ request failed: %d\n", __func__, ret);
		return;
	}

	cdata->c55_ap_int_gpio = gpio;
}

static int c55_ctrl_gpio_setup(struct c55_ctrl_data *cdata, struct device *dev)
{
	int i;

	if (of_gpio_count(dev->of_node) != NUM_GPIOS) {
		dev_err(dev, "%s: gpio count is not %d.\n", __func__, NUM_GPIOS);
		return -EINVAL;
	}

	for (i = 0; i < NUM_GPIOS; i++) {
		enum of_gpio_flags flags;
		int gpio;

		gpio = of_get_gpio_flags(dev->of_node, i, &flags);
		if (gpio < 0) {
			pr_err("%s: of_get_gpio failed: %d\n", __func__, gpio);
			return gpio;
		}

		gpio_request(gpio, gpio_labels[i]);

		gpio_export(gpio, false);
		gpio_export_link(dev, gpio_labels[i], gpio);

		if ((flags & GPIOF_IN) == GPIOF_IN) {
			gpio_direction_input(gpio);
			c55_ctrl_int_setup(cdata, gpio);
		} else {
			cdata->ap_c55_int_gpio = gpio;
			if ((flags & GPIOF_OUT_INIT_HIGH) == GPIOF_OUT_INIT_HIGH)
				gpio_direction_output(gpio, 1);
			else
				gpio_direction_output(gpio, 0);
		}
	}

	return 0;
}

static ssize_t c55_ctrl_enable(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct c55_ctrl_data *cdata = dev_get_drvdata(dev);
	struct m4sensorhub_data *m4sensorhub = m4sensorhub_client_get_drvdata();
	int mode;

	if (kstrtoint(buf, 10, &mode) < 0)
		return -EINVAL;

	if (mode >= C55_MODE_MAX) {
		dev_err(dev, "%s: Invalid mode %d\n", __func__, mode);
		return -EINVAL;
	}

	if (m4sensorhub->mode != NORMALMODE) {
		dev_err(dev, "M4 not ready, Unable to set screen status\n");
		return -EINVAL;
	}

	if (mode == cdata->c55_mode)
		return count;

	mutex_lock(&cdata->ctrl_mutex);

	if (mode == C55_ON) {
		pinctrl_select_state(cdata->pctrl, cdata->states[C55_ON]);

		gpio_set_value(cdata->ap_c55_int_gpio, 1);

		if (m4sensorhub_reg_write_1byte
		    (m4sensorhub, M4SH_REG_USERSETTINGS_AUDIOSTATUS,
		    AUDIO_STATUS_ON, 0xFF) != 1) {
			dev_err(dev, "Unable to set screen status to 0x01\n");
			mutex_unlock(&cdata->ctrl_mutex);
			return -EINVAL;
		}

		if (!cdata->c55_ap_int_enabled) {
			cdata->c55_ap_int_enabled = 1;
			enable_irq(__gpio_to_irq(cdata->c55_ap_int_gpio));
		}
	} else {
		/* Disable C55->AP IRQ when turning off C55 */
		if (cdata->c55_ap_int_enabled) {
			disable_irq_nosync(
				__gpio_to_irq(cdata->c55_ap_int_gpio));
			cdata->c55_ap_int_enabled = 0;
		}

		if (m4sensorhub_reg_write_1byte
		    (m4sensorhub, M4SH_REG_USERSETTINGS_AUDIOSTATUS,
		    AUDIO_STATUS_OFF, 0xFF) != 1) {
			dev_err(dev, "Unable to set screen status to 0x00\n");
			mutex_unlock(&cdata->ctrl_mutex);
			return -EINVAL;
		}

		/* AP->C55 interrupt needs to be set low when C55 is off
		 * for current drain reasons */
		gpio_set_value(cdata->ap_c55_int_gpio, 0);

		pinctrl_select_state(cdata->pctrl, cdata->states[C55_OFF]);

		/* Unlock wake lock in case it is active */
		wake_unlock(&cdata->wake_lock);
	}

	cdata->c55_mode = mode;

	mutex_unlock(&cdata->ctrl_mutex);

	dev_info(dev, "%s: enable = %d\n", __func__, mode);

	return count;
}

static DEVICE_ATTR(enable, S_IWUSR | S_IWGRP, NULL, c55_ctrl_enable);

static ssize_t c55_fw_verified_write(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct c55_ctrl_data *cdata = dev_get_drvdata(dev);
	int verified = 0;

	if (kstrtoint(buf, 10, &verified) < 0)
		return -EINVAL;

	mutex_lock(&cdata->ctrl_mutex);
	cdata->fw_verified = verified;
	mutex_unlock(&cdata->ctrl_mutex);

	return count;
}

static ssize_t c55_fw_verified_read(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct c55_ctrl_data *cdata = dev_get_drvdata(dev);
	int ret;

	mutex_lock(&cdata->ctrl_mutex);
	ret = sprintf(buf, "%d\n", cdata->fw_verified);
	mutex_unlock(&cdata->ctrl_mutex);

	return ret;
}

static DEVICE_ATTR(fw_verified, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP,
	c55_fw_verified_read, c55_fw_verified_write);

static int c55_ctrl_pin_setup(struct device *dev, struct c55_ctrl_data *cdata)
{
	int i, ret = 0;

	cdata->pctrl = devm_pinctrl_get(dev);
	if (IS_ERR(cdata->pctrl)) {
		ret = PTR_ERR(cdata->pctrl);
		dev_dbg(dev, "no pinctrl handle\n");
	}

	for (i = 0; !ret && (i < C55_MODE_MAX); i++) {
		cdata->states[i] = pinctrl_lookup_state(cdata->pctrl,
			c55_pin_state_labels[i]);
		if (IS_ERR(cdata->states[i])) {
			ret = PTR_ERR(cdata->states[i]);
			dev_dbg(dev, "no %s pinctrl state\n",
				c55_pin_state_labels[i]);
		}
	}

	if (!ret) {
		ret = pinctrl_select_state(cdata->pctrl,
			cdata->states[C55_OFF]);
		if (ret)
			dev_dbg(dev, "failed to activate %s pinctrl state\n",
				c55_pin_state_labels[C55_OFF]);
	}

	return ret;
}


static int c55_ctrl_probe(struct platform_device *pdev)
{
	struct c55_ctrl_data *cdata;
	int ret;
	if (!pdev->dev.of_node) {
		/* Platform data not currently supported */
		dev_err(&pdev->dev, "%s: of devtree data not found\n", __func__);
		return -EINVAL;
	}

	cdata = devm_kzalloc(&pdev->dev, sizeof(*cdata), GFP_KERNEL);
	if (cdata == NULL) {
		dev_err(&pdev->dev, "%s: devm_kzalloc failed.\n", __func__);
		return -ENOMEM;
	}

	mutex_init(&cdata->ctrl_mutex);

	ret = c55_ctrl_pin_setup(&pdev->dev, cdata);
	if (ret) {
		dev_err(&pdev->dev, "%s: c55_ctrl_pin_setup failed.\n",
			__func__);
		return ret;
	}

	cdata->c55_ap_int_gpio = -1;
	cdata->ap_c55_int_gpio = -1;
	ret = c55_ctrl_gpio_setup(cdata, &pdev->dev);

	if (ret) {
		dev_err(&pdev->dev, "%s: c55_ctrl_gpio_setup failed.\n", __func__);
		return ret;
	}

	cdata->reg_vddc = devm_regulator_get(&pdev->dev, "vddc");
	if (IS_ERR(cdata->reg_vddc))
		cdata->reg_vddc = NULL;
	else if (regulator_enable(cdata->reg_vddc))
		dev_err(&pdev->dev, "regulator_enable failed for vddc\n");

	cdata->reg_vddldo = devm_regulator_get(&pdev->dev, "vddldo");
	if (IS_ERR(cdata->reg_vddldo))
		cdata->reg_vddldo = NULL;
	else if (regulator_enable(cdata->reg_vddldo))
		dev_err(&pdev->dev, "regulator_enable failed for vddldo\n");

	/* M4 by default has C55 ON at power up */
	cdata->c55_mode = C55_ON;

	ret = device_create_file(&pdev->dev, &dev_attr_enable);
	if (ret) {
		dev_err(&pdev->dev, "%s: c55_ctrl creating set_mode failed.\n",
			__func__);
		return ret;
	}

	cdata->fw_verified = 0;

	ret = device_create_file(&pdev->dev, &dev_attr_fw_verified);
	if (ret) {
		dev_err(&pdev->dev, "%s: c55_ctrl creating fw_verified failed.\n",
			__func__);
		return ret;
	}

	wake_lock_init(&cdata->wake_lock, WAKE_LOCK_SUSPEND, "c55_ctrl");

	platform_set_drvdata(pdev, cdata);
	return 0;
}

static int c55_ctrl_remove(struct platform_device *pdev)
{
	device_remove_file(&pdev->dev, &dev_attr_enable);
	return 0;
}

#ifdef CONFIG_PM
static int c55_ctrl_suspend(struct platform_device *dev, pm_message_t state)
{
	struct c55_ctrl_data *cdata = dev_get_drvdata(&dev->dev);
	struct m4sensorhub_data *m4sensorhub = m4sensorhub_client_get_drvdata();

	if (cdata->c55_mode != C55_OFF) {
		dev_warn(&dev->dev, "C55 still ON when going into suspend\n");

		/* Disable C55->AP IRQ when turning off C55 */
		if (cdata->c55_ap_int_enabled) {
			disable_irq_nosync(
				__gpio_to_irq(cdata->c55_ap_int_gpio));
			cdata->c55_ap_int_enabled = 0;
		}

		if (m4sensorhub_reg_write_1byte
		    (m4sensorhub, M4SH_REG_USERSETTINGS_AUDIOSTATUS,
		    AUDIO_STATUS_OFF, 0xFF) != 1) {
			dev_err(&dev->dev,
				"Unable to set screen status to 0x00\n");
		}

		/* AP->C55 interrupt needs to be set low when C55 is off
		 * for current drain reasons */
		gpio_set_value(cdata->ap_c55_int_gpio, 0);

		cdata->c55_mode = C55_OFF;
	}

	pinctrl_select_state(cdata->pctrl, cdata->states[C55_OFF]);

	return 0;
}

static int c55_ctrl_resume(struct platform_device *dev)
{
	struct c55_ctrl_data *cdata = dev_get_drvdata(&dev->dev);

	pinctrl_select_state(cdata->pctrl, cdata->states[C55_ON]);

	return 0;
}
#else
#define c55_ctrl_suspend NULL
#define c55_ctrl_resume NULL
#endif

static struct of_device_id c55_ctrl_match[] = {
	{.compatible = "ti,c55-ctrl",},
	{},
};
MODULE_DEVICE_TABLE(of, c55_ctrl_match);

static const struct platform_device_id c55_ctrl_id_table[] = {
	{"c55_ctrl", 0},
	{},
};
MODULE_DEVICE_TABLE(of, c55_ctrl_id_table);

static struct platform_driver c55_ctrl_driver = {
	.driver = {
		.name = "c55_ctrl",
		.owner = THIS_MODULE,
		.of_match_table = c55_ctrl_match,
	},
	.probe = c55_ctrl_probe,
	.remove = c55_ctrl_remove,
	.suspend = c55_ctrl_suspend,
	.resume = c55_ctrl_resume,
	.id_table = c55_ctrl_id_table,
};

static int __init c55_ctrl_init(void)
{
	return platform_driver_register(&c55_ctrl_driver);
}

static void __exit c55_ctrl_exit(void)
{
	platform_driver_unregister(&c55_ctrl_driver);
}

module_init(c55_ctrl_init);
module_exit(c55_ctrl_exit);

MODULE_ALIAS("platform:c55_ctrl");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Motorola");
MODULE_DESCRIPTION("TI C55 control driver");