xref: /OK3568_Linux_fs/kernel/sound/soc/codecs/cx20810.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Driver for CX2081X voice capture IC.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright: Conexant Systems.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun  * it under the terms of the GNU General Public License version 2 as
9*4882a593Smuzhiyun  * published by the Free Software Foundation.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/i2c.h>
16*4882a593Smuzhiyun #include <linux/interrupt.h>
17*4882a593Smuzhiyun #include <linux/ioport.h>
18*4882a593Smuzhiyun #include <linux/delay.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/pm.h>
21*4882a593Smuzhiyun #include <linux/pm_runtime.h>
22*4882a593Smuzhiyun #include <linux/io.h>
23*4882a593Smuzhiyun #include <linux/platform_device.h>
24*4882a593Smuzhiyun #include <linux/timer.h>
25*4882a593Smuzhiyun #include <linux/jiffies.h>
26*4882a593Smuzhiyun #include <linux/delay.h>
27*4882a593Smuzhiyun #include <linux/of.h>
28*4882a593Smuzhiyun #include <linux/of_device.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <linux/of_gpio.h>
31*4882a593Smuzhiyun #include <linux/gpio.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include "cx20810_config.h"
34*4882a593Smuzhiyun 
i2c_send_array(struct i2c_client * client,const char * buf,int length)35*4882a593Smuzhiyun static int i2c_send_array(struct i2c_client *client,
36*4882a593Smuzhiyun 			  const char *buf, int length)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	int i, nwrite;
39*4882a593Smuzhiyun 	struct device *dev = &client->dev;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	for (i = 0; i < (length / 2); i++) {
42*4882a593Smuzhiyun 		nwrite = i2c_master_send(client, buf + i * 2, 2);
43*4882a593Smuzhiyun 		if (nwrite != 2) {
44*4882a593Smuzhiyun 			dev_err(dev, "i2c send configs error!/n");
45*4882a593Smuzhiyun 			return -1;
46*4882a593Smuzhiyun 		}
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 	return 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
cx20810_set_mode(struct device * dev,int mode,int index)51*4882a593Smuzhiyun static int cx20810_set_mode(struct device *dev, int mode, int index)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
54*4882a593Smuzhiyun 	int ret;
55*4882a593Smuzhiyun 	char *param;
56*4882a593Smuzhiyun 	int length;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	switch (mode) {
59*4882a593Smuzhiyun 	case CX20810_NORMAL_MODE:
60*4882a593Smuzhiyun 		param = codec_config_param_normal_mode;
61*4882a593Smuzhiyun 		length = sizeof(codec_config_param_normal_mode);
62*4882a593Smuzhiyun 		break;
63*4882a593Smuzhiyun 	case CX20810_NORMAL_MODE2:
64*4882a593Smuzhiyun 		param = codec_config_param_normal_mode2;
65*4882a593Smuzhiyun 		length = sizeof(codec_config_param_normal_mode2);
66*4882a593Smuzhiyun 		break;
67*4882a593Smuzhiyun 	case CX20810_NORMAL_MODE_SIMPLE:
68*4882a593Smuzhiyun 		param = codec_config_param_normal_mode_simple;
69*4882a593Smuzhiyun 		length = sizeof(codec_config_param_normal_mode_simple);
70*4882a593Smuzhiyun 		break;
71*4882a593Smuzhiyun 	case CX20810_48K_16BIT_MODE:
72*4882a593Smuzhiyun 		param = codec_config_param_48k_16bit_mode;
73*4882a593Smuzhiyun 		length = sizeof(codec_config_param_48k_16bit_mode);
74*4882a593Smuzhiyun 		break;
75*4882a593Smuzhiyun 	case CX20810_96K_16BIT_MODE:
76*4882a593Smuzhiyun 		param = codec_config_param_96k_16bit_mode;
77*4882a593Smuzhiyun 		length = sizeof(codec_config_param_96k_16bit_mode);
78*4882a593Smuzhiyun 		break;
79*4882a593Smuzhiyun 	case CX20810_NIRMAL_MODE_CODEC3:
80*4882a593Smuzhiyun 		param = codec3_config_param_normal_mode;
81*4882a593Smuzhiyun 		length = sizeof(codec3_config_param_normal_mode);
82*4882a593Smuzhiyun 		break;
83*4882a593Smuzhiyun 	case CX20810_NIRMAL_MODE_CODEC3_SIMPLE:
84*4882a593Smuzhiyun 		param = codec3_config_param_normal_mode_simple;
85*4882a593Smuzhiyun 		length = sizeof(codec3_config_param_normal_mode_simple);
86*4882a593Smuzhiyun 		break;
87*4882a593Smuzhiyun 	default:
88*4882a593Smuzhiyun 		dev_err(dev, "Illegal mode.\n");
89*4882a593Smuzhiyun 		return 0;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	ret = i2c_send_array(client,	param, length);
93*4882a593Smuzhiyun 	if (ret != 0) {
94*4882a593Smuzhiyun 		dev_err(dev, "cx2081x send configs failed!\n");
95*4882a593Smuzhiyun 		return -1;
96*4882a593Smuzhiyun 	}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	return 0;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /*  initial cx20810 */
cx20810_init(struct device * dev,int index,int mode)102*4882a593Smuzhiyun static void cx20810_init(struct device *dev, int index, int mode)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	if (cx20810_set_mode(dev, mode, index) < 0)
105*4882a593Smuzhiyun 		dev_err(dev, "cx20810 set mode fail.\n");
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun static const struct of_device_id of_cx2081x_match[] = {
109*4882a593Smuzhiyun 	{ .compatible = "conexant,cx20810" },
110*4882a593Smuzhiyun 	{ .compatible = "conexant,cx20811" },
111*4882a593Smuzhiyun 	{},
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, of_cx2081x_match);
115*4882a593Smuzhiyun 
cx2081x_probe(struct i2c_client * client,const struct i2c_device_id * id)116*4882a593Smuzhiyun static int cx2081x_probe(struct i2c_client *client,
117*4882a593Smuzhiyun 			 const struct i2c_device_id *id)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	const struct of_device_id *match;
120*4882a593Smuzhiyun 	struct device *dev = &client->dev;
121*4882a593Smuzhiyun 	struct gpio_desc *reset_gpio;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	if (dev->of_node) {
124*4882a593Smuzhiyun 		match = of_match_device(of_cx2081x_match, dev);
125*4882a593Smuzhiyun 		if (!match) {
126*4882a593Smuzhiyun 			dev_err(dev, "Failed to find matching dt id\n");
127*4882a593Smuzhiyun 			return -EINVAL;
128*4882a593Smuzhiyun 		}
129*4882a593Smuzhiyun 	}
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	reset_gpio = gpiod_get_optional(dev,
132*4882a593Smuzhiyun 					"reset", GPIOD_OUT_LOW);
133*4882a593Smuzhiyun 	if (IS_ERR(reset_gpio)) {
134*4882a593Smuzhiyun 		int error = PTR_ERR(reset_gpio);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		dev_err(dev, "Failed to request RESET gpio: %d\n", error);
137*4882a593Smuzhiyun 	} else {
138*4882a593Smuzhiyun 		gpiod_set_value(reset_gpio, 0);
139*4882a593Smuzhiyun 		mdelay(10);
140*4882a593Smuzhiyun 		gpiod_set_value(reset_gpio, 1);
141*4882a593Smuzhiyun 		/*
142*4882a593Smuzhiyun 		 * we found there are possibilities download configs through
143*4882a593Smuzhiyun 		 * i2c failed, some delay can fix it.
144*4882a593Smuzhiyun 		 */
145*4882a593Smuzhiyun 		mdelay(10);
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	cx20810_init(dev, 0, CX20810_NORMAL_MODE);
149*4882a593Smuzhiyun 	if (!IS_ERR(reset_gpio))
150*4882a593Smuzhiyun 		gpiod_put(reset_gpio);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	return 0;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun static const struct i2c_device_id cx2081x_id[] = {
156*4882a593Smuzhiyun 	{"cx20810"},
157*4882a593Smuzhiyun 	{"cx20811"},
158*4882a593Smuzhiyun 	{}
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, cx20810_id);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun static struct i2c_driver cx2081x_driver = {
164*4882a593Smuzhiyun 	.driver         = {
165*4882a593Smuzhiyun 		.name   = "cx2081x",
166*4882a593Smuzhiyun 		.of_match_table = of_cx2081x_match,
167*4882a593Smuzhiyun 	},
168*4882a593Smuzhiyun 	.probe          = cx2081x_probe,
169*4882a593Smuzhiyun 	.id_table       = cx2081x_id,
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun module_i2c_driver(cx2081x_driver);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun MODULE_AUTHOR("Timothy");
175*4882a593Smuzhiyun MODULE_DESCRIPTION("I2C device cx20810 loader");
176*4882a593Smuzhiyun MODULE_LICENSE("GPL");
177