tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
 * acx00-core.c  --  Device access for allwinnertech acx00
 *
 * Copyright (C) 2017 Allwinner.
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */
 
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/mfd/core.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/mfd/acx00-mfd.h>
#include <linux/pwm.h>
#include <linux/regulator/consumer.h>
/*#include <linux/sys_config.h>*/
#include <linux/sunxi-sid.h>
 
#define SUNXI_CHIP_NAME    "ACX00-CHIP"
static unsigned int twi_id;
atomic_t acx00_en;
 
struct regmap_config acx00_base_regmap_config = {
   .reg_bits = 8,
   .val_bits = 16,
};
static struct regulator *vcc_ave;
static char key_name[20] = "ac200";
static char ave_regulator_name[20] = "vcc-audio-33";
static int tv_twi_used;
#if defined(CONFIG_ARCH_SUN50IW9)
static struct pwm_device *pwm;
#endif
 
static int sys_script_get_item(char *main_name, char *sub_name, int value[],
                  int type);
/**
 * acx00_reg_read: Read a single ACX00 register.
 *
 * @acx00: Device to read from.
 * @reg: Register to read.
 */
int acx00_reg_read(struct acx00 *acx00, unsigned short reg)
{
   unsigned int val;
   int ret;
 
   mutex_lock(&acx00->lock);
   regmap_write(acx00->regmap, 0xfe, reg>>8);
   ret = regmap_read(acx00->regmap, reg&0xff, &val);
   mutex_unlock(&acx00->lock);
   if (ret < 0)
       return ret;
   else
       return val;
}
EXPORT_SYMBOL_GPL(acx00_reg_read);
 
/**
 * acx00_reg_write: Write a single ACX00 register.
 *
 * @acx00: Device to write to.
 * @reg: Register to write to.
 * @val: Value to write.
 */
int acx00_reg_write(struct acx00 *acx00, unsigned short reg,
            unsigned short val)
{
   int ret;
 
   mutex_lock(&acx00->lock);
   regmap_write(acx00->regmap, 0xfe, reg>>8);
   ret = regmap_write(acx00->regmap, reg&0xff, val);
   mutex_unlock(&acx00->lock);
 
   return ret;
}
EXPORT_SYMBOL_GPL(acx00_reg_write);
 
int acx00_enable(void)
{
   return atomic_read(&acx00_en);
}
EXPORT_SYMBOL_GPL(acx00_enable);
 
#if defined(CONFIG_ARCH_SUN50IW6)
/**
 * @name       acx00_read_sid
 * @brief      read tv out sid from efuse
 * @param[IN]   none
 * @param[OUT]  p_bang_gap:tve band gap
 * @return    return 0 if success,-1 if fail
 */
static s32 acx00_read_sid(u16 *p_band_gap)
{
   s32 ret = 0;
   u8 buf[6];
 
   if (p_band_gap == NULL) {
       pr_err("%s's pointer type args are NULL!\n", __func__);
       return -1;
   }
   ret = sunxi_efuse_readn(EFUSE_OEM_NAME, buf, 6);
   if (ret < 0) {
       pr_err("sunxi_efuse_readn failed:%d\n", ret);
       return ret;
   }
   *p_band_gap = buf[4] + (buf[5] << 8);
   return 0;
}
#endif
 
static void acx00_init_work(struct work_struct *work)
{
   struct acx00 *acx00 = container_of(work, struct acx00, init_work);
#if defined(CONFIG_ARCH_SUN50IW6)
   u16 band_gap = 0;
#endif
 
   atomic_set(&acx00_en, 0);
   pr_err("%s,l:%d\n", __func__, __LINE__);
#if defined(CONFIG_ARCH_SUN50IW6)
   acx00_read_sid(&band_gap);
       acx00_reg_write(acx00, 0x0050, band_gap | 0x8000 | (0xa << 6));
   msleep(120);
#endif /*endif CONFIG_ARCH_SUN50IW6 */
   acx00_reg_write(acx00, 0x0002, 0x1);
   pr_err("%s,l:%d\n", __func__, __LINE__);
   atomic_set(&acx00_en, 1);
}
 
static struct mfd_cell acx00_devs[] = {
   {
       .name = "acx00-codec",
   },
   {
       .name = "rtc0_ac200",
   },
   {
       .name = "tv",
   },
   {
       .name = "acx-ephy",
   },
};
 
/*
 * Instantiate the generic non-control parts of the device.
 */
static int acx00_device_init(struct acx00 *acx00, int irq)
{
   int ret;
 
   dev_set_drvdata(acx00->dev, acx00);
   ret = mfd_add_devices(acx00->dev, -1,
                 acx00_devs, ARRAY_SIZE(acx00_devs),
                 NULL, 0, NULL);
   if (ret != 0) {
       dev_err(acx00->dev, "Failed to add children: %d\n", ret);
       goto err;
   }
   return 0;
 
err:
   mfd_remove_devices(acx00->dev);
   return ret;
}
 
static void acx00_device_exit(struct acx00 *acx00)
{
   mfd_remove_devices(acx00->dev);
}
/**************************read reg interface**************************/
static ssize_t acx00_dump_store(struct device *dev,
   struct device_attribute *attr, const char *buf, size_t count)
{
   static int val;
   int reg, num, i = 0;
   int value_r[128];
   s32 ret = -1;
   struct acx00 *acx00 = dev_get_drvdata(dev);
 
   ret = kstrtoint(buf, 16, &val);
   if (ret != 0) {
       dev_err(dev, "kstrtoint fail!\n");
       return ret;
   }
   reg = (val >> 8);
   num = val & 0xff;
   dev_info(dev, "\n");
   dev_info(dev, "read:start add:0x%x,count:0x%x\n", reg, num);
   do {
       value_r[i] = acx00_reg_read(acx00, reg);
       dev_info(dev, "0x%x: 0x%04x ", reg, value_r[i]);
       reg += 1;
       i++;
       if (i == num)
           dev_info(dev, "\n");
       if (i % 4 == 0)
           dev_info(dev, "\n");
   } while (i < num);
 
   return count;
}
 
static ssize_t acx00_dump_show(struct device *dev,
   struct device_attribute *attr, char *buf)
{
   dev_info(dev, "echo reg|count > dump\n");
   dev_info(dev, "eg read star address=0x0006,count 0x10:echo 0x610 > dump\n");
   dev_info(dev, "eg read star address=0x2000,count 0x10:echo 0x200010 > dump\n");
 
   return 0;
}
 
/**************************write reg interface**************************/
static ssize_t acx00_write_store(struct device *dev,
   struct device_attribute *attr, const char *buf, size_t count)
{
   static int val;
   int reg;
   int value_w;
   s32 ret = -1;
   struct acx00 *acx00 = dev_get_drvdata(dev);
 
   ret = kstrtoint(buf, 16, &val);
   if (ret != 0) {
       dev_err(dev, "kstrtoint fail!\n");
       return ret;
   }
 
   reg = (val >> 16);
   value_w = val & 0xFFFF;
   acx00_reg_write(acx00, reg, value_w);
   dev_info(dev, "write 0x%x to reg:0x%x\n", value_w, reg);
 
   return count;
}
 
static ssize_t acx00_write_show(struct device *dev,
   struct device_attribute *attr, char *buf)
{
   dev_info(dev, "echo reg|val > write\n");
   printk(
       KERN_INFO
       "eg write value:0x13fe to address:0x0004 :echo 0x413fe > write\n");
   printk(
       KERN_INFO
       "eg write value:0x6 to address:0x2000 :echo 0x20000006 > write\n");
 
   return 0;
}
 
static DEVICE_ATTR(dump, 0644, acx00_dump_show, acx00_dump_store);
static DEVICE_ATTR(write, 0644, acx00_write_show, acx00_write_store);
static struct attribute *audio_debug_attrs[] = {
   &dev_attr_dump.attr,
   &dev_attr_write.attr,
   NULL,
};
static struct attribute_group audio_debug_attr_group = {
   .name   = "acx00_debug",
   .attrs  = audio_debug_attrs,
};
 
static int acx00_i2c_probe(struct i2c_client *i2c,
              const struct i2c_device_id *id)
{
   struct acx00 *acx00;
   int ret = 0;
   int value;
 
   pr_err("%s,l:%d\n", __func__, __LINE__);
   acx00 = devm_kzalloc(&i2c->dev, sizeof(struct acx00), GFP_KERNEL);
   if (acx00 == NULL)
       return -ENOMEM;
 
   i2c_set_clientdata(i2c, acx00);
   acx00->dev = &i2c->dev;
   acx00->irq = i2c->irq;
   mutex_init(&acx00->lock);
 
   acx00->regmap = devm_regmap_init_i2c(i2c, &acx00_base_regmap_config);
   if (IS_ERR(acx00->regmap)) {
       ret = PTR_ERR(acx00->regmap);
       dev_err(acx00->dev, "Failed to allocate register map: %d\n",
           ret);
       return ret;
   }
   ret = sysfs_create_group(&i2c->dev.kobj, &audio_debug_attr_group);
   if (ret)
       pr_err("failed to create attr group\n");
   INIT_WORK(&acx00->init_work, acx00_init_work);
 
#if defined(CONFIG_ARCH_SUN50IW9)
   if (!pwm) {
       pr_err("[ac200] pwm is NULL! Just initialize it.\n");
       ret = sys_script_get_item(key_name, "tv_pwm_ch", &value, 1);
       if (ret == 1) {
           pwm = pwm_request(value, NULL);
           if (!IS_ERR_OR_NULL(pwm)) {
               pwm_config(pwm, 205, 410);
               pwm_enable(pwm);
               pr_err("[ac200] pwm enable\n");
 
               acx00->pwm_ac200 = pwm;
               pr_err("[ac200] pwm is initialized\n");
           } else {
               pr_err("[ac200] can't get pwm device\n");
           }
       } else {
           pr_err("[ac200] Get tv_pwm_ch failed\n");
       }
   } else {
       acx00->pwm_ac200 = pwm;
       pr_err("[ac200] pwm is initialized\n");
   }
#else
   ret = sys_script_get_item(key_name, "tv_pwm_ch", &value, 1);
   if (ret == 1) {
       acx00->pwm_ac200 = pwm_request(value, NULL);
       if (!IS_ERR_OR_NULL(acx00->pwm_ac200)) {
           pwm_config(acx00->pwm_ac200, 20, 41);
           pwm_enable(acx00->pwm_ac200);
       } else
           dev_warn(acx00->dev, "Warn: can't get pwm device\n");
   } else {
       dev_warn(acx00->dev, "Get tv_pwm_ch failed\n");
   }
#endif
 
   atomic_set(&acx00_en, 0);
   schedule_work(&acx00->init_work);
 
   return acx00_device_init(acx00, i2c->irq);
}
 
static int acx00_i2c_remove(struct i2c_client *i2c)
{
   struct acx00 *acx00 = i2c_get_clientdata(i2c);
 
   sysfs_remove_group(&i2c->dev.kobj, &audio_debug_attr_group);
   acx00_device_exit(acx00);
 
   if (!IS_ERR_OR_NULL(acx00->pwm_ac200)) {
       pwm_disable(acx00->pwm_ac200);
       pwm_free(acx00->pwm_ac200);
   }
 
   return 0;
}
static int acx00_i2c_suspend(struct device *dev)
{
   struct acx00 *acx00 = dev_get_drvdata(dev);
 
   if (vcc_ave != NULL) {
       regulator_disable(vcc_ave);
       regulator_put(vcc_ave);
       vcc_ave = NULL;
   }
 
   if (!IS_ERR_OR_NULL(acx00->pwm_ac200))
       pwm_disable(acx00->pwm_ac200);
 
   atomic_set(&acx00_en, 0);
   return 0;
}
 
static int acx00_i2c_resume(struct device *dev)
{
   int ret = 0;
   struct acx00 *acx00 = dev_get_drvdata(dev);
#if defined(CONFIG_ARCH_SUN50IW6)
   u16 band_gap = 0;
#endif
 
   vcc_ave = regulator_get(NULL, ave_regulator_name);
   if (IS_ERR_OR_NULL(vcc_ave)) {
       pr_err("get audio %s failed\n", ave_regulator_name);
   } else {
       ret = regulator_enable(vcc_ave);
       if (IS_ERR(vcc_ave)) {
           pr_err("[%s]: vcc_ave:regulator_enable() failed!\n",
                  __func__);
       }
   }
 
   if (!IS_ERR_OR_NULL(acx00->pwm_ac200)) {
#if defined(CONFIG_ARCH_SUN50IW9)
       pwm_config(acx00->pwm_ac200, 205, 410);
#else
       pwm_config(acx00->pwm_ac200, 20, 41);
#endif
       pwm_enable(acx00->pwm_ac200);
   }
 
   atomic_set(&acx00_en, 0);
   msleep(40);
   pr_err("%s,l:%d\n", __func__, __LINE__);
#if defined(CONFIG_ARCH_SUN50IW6)
   if (acx00_read_sid(&band_gap) == 0 && band_gap != 0)
       acx00_reg_write(acx00, 0x0050, band_gap | 0x8000 | (0xa << 6));
#endif /*endif CONFIG_ARCH_SUN50IW6 */
   acx00_reg_write(acx00, 0x0002, 0x1);
   pr_err("%s,l:%d\n", __func__, __LINE__);
   atomic_set(&acx00_en, 1);
   return 0;
}
 
static const struct dev_pm_ops acx00_core_pm_ops = {
   .suspend_late = acx00_i2c_suspend,
   .resume_early = acx00_i2c_resume,
};
 
static int acx00_detect(struct i2c_client *client, struct i2c_board_info *info)
{
   struct i2c_adapter *adapter = client->adapter;
 
   pr_info("%s, l:%d, twi_id:%d, adapter->nr:%d\n", __func__, __LINE__,
       twi_id, adapter->nr);
   if (twi_id == adapter->nr) {
       strlcpy(info->type, SUNXI_CHIP_NAME, I2C_NAME_SIZE);
       return 0;
   } else {
       return -ENODEV;
   }
}
 
static unsigned short normal_i2c[] = {0x10, I2C_CLIENT_END};
 
static const struct i2c_device_id acx00_id[] = {
   {"ACX00-CHIP", 2},
   {}
};
 
static const struct of_device_id sunxi_ac200_match[] = {
   { .compatible = "allwinner,sunxi-ac200", },
   {},
};
 
static struct i2c_driver acx00_i2c_driver = {
   .class = I2C_CLASS_HWMON,
   .id_table = acx00_id,
   .probe = acx00_i2c_probe,
   .remove = acx00_i2c_remove,
   .driver = {
       .owner = THIS_MODULE,
       .name = "ACX00-CHIP",
       .of_match_table = sunxi_ac200_match,
       .pm = &acx00_core_pm_ops,
   },
   .address_list = normal_i2c,
};
 
/* type: 0:invalid, 1: int; 2:str*/
static int sys_script_get_item(char *main_name, char *sub_name, int value[],
                  int type)
{
   char compat[32];
   u32 len = 0;
   struct device_node *node;
   int ret = 0;
 
   len = sprintf(compat, "allwinner,sunxi-%s", main_name);
   if (len > 32) {
       pr_warn("size of mian_name is out of range\n");
       goto error_exit;
   }
 
   node = of_find_compatible_node(NULL, NULL, compat);
   if (!node) {
       pr_warn("of_find_compatible_node %s fail\n", compat);
       goto error_exit;
   }
 
   if (type == 1) {
       if (of_property_read_u32_array(node, sub_name, value, 1)) {
           pr_info("of_property_read_u32_array %s.%s fail\n",
               main_name, sub_name);
           goto error_exit;
       } else
           ret = type;
   } else if (type == 2) {
       const char *str;
 
       if (of_property_read_string(node, sub_name, &str)) {
           pr_info("of_property_read_string %s.%s fail\n",
               main_name, sub_name);
           goto error_exit;
       } else {
           ret = type;
           memcpy((void *)value, str, strlen(str) + 1);
       }
   }
 
   return ret;
error_exit:
   return -1;
}
 
static int __init acx00_i2c_init(void)
{
   int ret = 0;
   int value;
   int ac200_used = 0;
 
   ret = sys_script_get_item(key_name, "tv_used", &value, 1);
   if (ret == 1)
       ac200_used = value;
 
   if (ac200_used) {
       ret = sys_script_get_item(key_name, "tv_twi_used",
               &value, 1);
       if (ret == 1)
           tv_twi_used = value;
       if (tv_twi_used == 1) {
           ret = sys_script_get_item(key_name, "tv_twi_id",
                   &value, 1);
           twi_id = (ret == 1) ? value : twi_id;
           ret = sys_script_get_item(key_name, "tv_twi_addr",
                         &value, 1);
           normal_i2c[0] = (ret == 1) ? value : normal_i2c[0];
           acx00_i2c_driver.detect = acx00_detect;
           ret = i2c_add_driver(&acx00_i2c_driver);
           if (ret != 0)
               pr_err(
                   "Failed to register acx00 I2C driver: %d\n",
                   ret);
       }
       ret = sys_script_get_item(key_name, "tv_regulator_name",
                     (int *)&ave_regulator_name, 2);
       if (ret == 2) {
           vcc_ave = regulator_get(NULL, ave_regulator_name);
           if (IS_ERR_OR_NULL(vcc_ave)) {
               pr_err("get audio %s failed\n",
                      ave_regulator_name);
           } else {
               ret = regulator_enable(vcc_ave);
               if (IS_ERR(vcc_ave)) {
                   pr_err(
                       "[%s]: vcc_ave enable failed!\n",
                       __func__);
               }
           }
       } else {
           pr_err("[ac200] get ave_regulator_name failed!\n");
       }
#if defined(CONFIG_ARCH_SUN50IW9)
       if (!pwm) {
           ret = sys_script_get_item(key_name, "tv_pwm_ch", &value, 1);
           if (ret == 1) {
               pwm = pwm_request(value, NULL);
               if (!IS_ERR_OR_NULL(pwm)) {
                   pwm_config(pwm, 205, 410);
                   pwm_enable(pwm);
                   pr_err("[ac200] pwm enable\n");
               } else {
                   pr_err("[ac200] can't get pwm device\n");
               }
           } else {
               pr_err("[ac200] Get tv_pwm_ch failed\n");
           }
       }
#endif
   }
 
   return ret;
}
subsys_initcall_sync(acx00_i2c_init);
 
static void __exit acx00_i2c_exit(void)
{
   i2c_del_driver(&acx00_i2c_driver);
}
module_exit(acx00_i2c_exit);
 
MODULE_DESCRIPTION("Core support for the ACX00X00 audio CODEC");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("huangxin<huangxin@allwinnertech.com>");