1 #include <linux/module.h>
2 #include <linux/init.h>
4 #include <linux/device.h>
5 #include <linux/delay.h>
8 #include <linux/gpio.h>
9 #include <linux/slab.h>
10 #include <linux/mfd/core.h>
11 #include <linux/mfd/rk1000.h>
13 #include <linux/of_gpio.h>
14 #include <linux/regulator/consumer.h>
16 #define RK1000_CORE_DBG 0
19 #define DBG(x...) pr_info(x)
25 #define CTRL_CODEC 0x01
28 #define RGB2CCIR_RESET 0x04
29 #define ADC_START 0x05
32 struct i2c_client *client;
34 struct dentry *debugfs_dir;
35 struct ioctrl io_power;
36 struct ioctrl io_reset;
39 static struct rk1000 *rk1000;
42 void rk1000_reset_ctrl(int enable)
44 DBG("rk1000_reset_ctrl\n");
45 if (rk1000 && gpio_is_valid(rk1000->io_reset.gpio)) {
47 gpio_set_value(rk1000->io_reset.gpio,
48 !(rk1000->io_reset.active));
50 DBG("rk1000 reset pull low\n");
51 gpio_set_value(rk1000->io_reset.gpio,
52 (rk1000->io_reset.active));
57 int rk1000_i2c_send(const u8 addr, const u8 reg, const u8 value)
59 struct i2c_adapter *adap;
64 if (rk1000 == NULL || rk1000->client == NULL) {
65 DBG("rk1000 not init!\n");
68 adap = rk1000->client->adapter;
72 msg.flags = rk1000->client->flags;
75 msg.scl_rate = RK1000_I2C_RATE;
76 ret = i2c_transfer(adap, &msg, 1);
78 DBG("rk1000 control i2c write err,ret =%d\n", ret);
84 int rk1000_i2c_recv(const u8 addr, const u8 reg, const char *buf)
86 struct i2c_adapter *adap;
87 struct i2c_msg msgs[2];
90 if (rk1000 == NULL || rk1000->client == NULL) {
91 DBG("rk1000 not init!\n");
94 adap = rk1000->client->adapter;
96 msgs[0].flags = rk1000->client->flags;
98 msgs[0].buf = (unsigned char *)(®);
99 msgs[0].scl_rate = RK1000_I2C_RATE;
101 msgs[1].flags = rk1000->client->flags | I2C_M_RD;
103 msgs[1].buf = (unsigned char *)buf;
104 msgs[1].scl_rate = RK1000_I2C_RATE;
105 ret = i2c_transfer(adap, msgs, 2);
106 return (ret == 2) ? 0 : -1;
109 static ssize_t rk1000_show(struct device *dev,
110 struct device_attribute *attr,
115 unsigned char tv_encoder_regs[] = {0x00, 0x00, 0x00, 0x03, 0x00, 0x00};
116 unsigned char tv_encoder_control_regs[] = {0x43, 0x01};
118 for (i = 0; i < sizeof(tv_encoder_regs); i++) {
119 ret = rk1000_i2c_recv(I2C_ADDR_TVE, i, buf);
120 pr_info("---%x--\n", buf[0]);
122 pr_err("rk1000_tv_write_block err!\n");
127 for (i = 0; i < sizeof(tv_encoder_control_regs); i++) {
128 ret = rk1000_i2c_recv(I2C_ADDR_CTRL, i + 3, buf);
129 pr_info("cntrl---%x--\n", buf[0]);
131 pr_err("rk1000_control_write_block err!\n");
138 static DEVICE_ATTR(rkcontrl, S_IRUGO, rk1000_show, NULL);
141 static int __init bootloader_cvbs_setup(char *str)
146 pr_info("cvbs init tve.format is %s\n", str);
147 ret = kstrtoint(str, 0, &cvbsmode);
151 early_param("tve.format", bootloader_cvbs_setup);
154 static int rk1000_control_suspend(struct device *dev)
158 DBG("rk1000_control_suspend\n");
159 ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x22);
160 DBG("ret=0x%x\n", ret);
161 ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x00);
162 DBG("ret=0x%x\n", ret);
163 ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x07);
164 DBG("ret=0x%x\n", ret);
165 /* rk1000_reset_ctrl(0); */
169 static int rk1000_control_resume(struct device *dev)
173 /* rk1000_reset_ctrl(1); */
174 DBG("rk1000_control_resume\n");
176 ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_ADC, 0x88);
177 DBG("ret=0x%x\n", ret);
178 #ifdef CONFIG_SND_SOC_RK1000
179 ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x00);
181 ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x0d);
183 DBG("ret=0x%x\n", ret);
184 rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_I2C, 0x22);
185 DBG("ret=0x%x\n", ret);
186 /* rk1000_codec_reg_set(); */
193 static int rk1000_probe(struct i2c_client *client,
194 const struct i2c_device_id *id)
196 struct device_node *rk1000_np;
197 enum of_gpio_flags flags;
200 DBG("[%s] start\n", __func__);
201 rk1000 = kmalloc(sizeof(*rk1000), GFP_KERNEL);
203 dev_err(&client->dev, ">> rk1000 core inf kmalloc fail!");
207 memset(rk1000, 0, sizeof(struct rk1000));
208 rk1000->client = client;
209 rk1000->dev = &client->dev;
210 rk1000_np = rk1000->dev->of_node;
213 /********Get reset pin***********/
214 rk1000->io_reset.gpio = of_get_named_gpio_flags(rk1000_np,
217 if (!gpio_is_valid(rk1000->io_reset.gpio)) {
218 DBG("invalid rk1000->io_reset.gpio: %d\n",
219 rk1000->io_reset.gpio);
223 ret = gpio_request(rk1000->io_reset.gpio, "rk1000-reset-io");
225 DBG("gpio_request rk1000->io_reset.gpio invalid: %d\n",
226 rk1000->io_reset.gpio);
229 rk1000->io_reset.active = !(flags & OF_GPIO_ACTIVE_LOW);
230 gpio_direction_output(rk1000->io_reset.gpio,
231 !(rk1000->io_reset.active));
232 usleep_range(500, 1000);
233 /********Get power pin***********/
234 rk1000->io_power.gpio = of_get_named_gpio_flags(rk1000_np,
237 if (gpio_is_valid(rk1000->io_power.gpio)) {
238 ret = gpio_request(rk1000->io_power.gpio,
241 DBG("request gpio for power invalid: %d\n",
242 rk1000->io_power.gpio);
245 rk1000->io_power.active =
246 !(flags & OF_GPIO_ACTIVE_LOW);
247 gpio_direction_output(rk1000->io_power.gpio,
248 rk1000->io_power.active);
250 /********rk1000 reset***********/
251 gpio_set_value(rk1000->io_reset.gpio,
252 rk1000->io_reset.active);
253 usleep_range(5000, 10000);
254 gpio_set_value(rk1000->io_reset.gpio,
255 !(rk1000->io_reset.active));
257 rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_ADC, 0x88);
258 #ifdef CONFIG_SND_SOC_RK1000
259 rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x00);
261 rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x0d);
263 rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_I2C, 0x22);
266 rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x00);
268 device_create_file(&client->dev, &dev_attr_rkcontrl);
269 DBG("rk1000 probe ok\n");
277 static int rk1000_remove(struct i2c_client *client)
282 static const struct i2c_device_id rk1000_id[] = {
283 { "rk1000_control", 0 },
286 MODULE_DEVICE_TABLE(i2c, rk1000_id);
288 static const struct dev_pm_ops rockchip_rk1000_pm_ops = {
289 .suspend_late = rk1000_control_suspend,
290 .resume_early = rk1000_control_resume,
293 static struct i2c_driver rk1000_driver = {
295 .name = "rk1000_control",
297 .pm = &rockchip_rk1000_pm_ops,
300 .probe = rk1000_probe,
301 .remove = rk1000_remove,
302 .id_table = rk1000_id,
306 static int __init rk1000_init(void)
308 return i2c_add_driver(&rk1000_driver);
311 static void __exit rk1000_exit(void)
313 i2c_del_driver(&rk1000_driver);
316 fs_initcall_sync(rk1000_init);
317 module_exit(rk1000_exit);
320 MODULE_DESCRIPTION("RK1000 control driver");
321 MODULE_AUTHOR("Rock-chips, <www.rock-chips.com>");
322 MODULE_LICENSE("GPL");