1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19#include <linux/i2c.h>
20#include <linux/gpio.h>
21#include <linux/regmap.h>
22#include <linux/slab.h>
23#include <linux/platform_device.h>
24#include <linux/regulator/consumer.h>
25#include <linux/gpio/consumer.h>
26#include <linux/of_device.h>
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
31#include <sound/soc-dapm.h>
32#include <sound/initval.h>
33#include <sound/tlv.h>
34#include <dt-bindings/sound/cs35l32.h>
35
36#include "cs35l32.h"
37
38#define CS35L32_NUM_SUPPLIES 2
39static const char *const cs35l32_supply_names[CS35L32_NUM_SUPPLIES] = {
40 "VA",
41 "VP",
42};
43
44struct cs35l32_private {
45 struct regmap *regmap;
46 struct snd_soc_component *component;
47 struct regulator_bulk_data supplies[CS35L32_NUM_SUPPLIES];
48 struct cs35l32_platform_data pdata;
49 struct gpio_desc *reset_gpio;
50};
51
52static const struct reg_default cs35l32_reg_defaults[] = {
53
54 { 0x06, 0x04 },
55 { 0x07, 0xE8 },
56 { 0x08, 0x40 },
57 { 0x09, 0x20 },
58 { 0x0A, 0x00 },
59 { 0x0B, 0x40 },
60 { 0x0C, 0x07 },
61 { 0x0D, 0x03 },
62 { 0x0F, 0x20 },
63 { 0x10, 0x14 },
64 { 0x11, 0x00 },
65 { 0x12, 0xFF },
66 { 0x13, 0xFF },
67 { 0x14, 0xFF },
68 { 0x19, 0x00 },
69 { 0x1A, 0x00 },
70 { 0x1B, 0x20 },
71 { 0x1C, 0x00 },
72};
73
74static bool cs35l32_readable_register(struct device *dev, unsigned int reg)
75{
76 switch (reg) {
77 case CS35L32_DEVID_AB ... CS35L32_AUDIO_LED_MNGR:
78 case CS35L32_ADSP_CTL ... CS35L32_FLASH_INHIBIT:
79 return true;
80 default:
81 return false;
82 }
83}
84
85static bool cs35l32_volatile_register(struct device *dev, unsigned int reg)
86{
87 switch (reg) {
88 case CS35L32_DEVID_AB ... CS35L32_REV_ID:
89 case CS35L32_INT_STATUS_1 ... CS35L32_LED_STATUS:
90 return true;
91 default:
92 return false;
93 }
94}
95
96static bool cs35l32_precious_register(struct device *dev, unsigned int reg)
97{
98 switch (reg) {
99 case CS35L32_INT_STATUS_1 ... CS35L32_LED_STATUS:
100 return true;
101 default:
102 return false;
103 }
104}
105
106static DECLARE_TLV_DB_SCALE(classd_ctl_tlv, 900, 300, 0);
107
108static const struct snd_kcontrol_new imon_ctl =
109 SOC_DAPM_SINGLE("Switch", CS35L32_PWRCTL2, 6, 1, 1);
110
111static const struct snd_kcontrol_new vmon_ctl =
112 SOC_DAPM_SINGLE("Switch", CS35L32_PWRCTL2, 7, 1, 1);
113
114static const struct snd_kcontrol_new vpmon_ctl =
115 SOC_DAPM_SINGLE("Switch", CS35L32_PWRCTL2, 5, 1, 1);
116
117static const struct snd_kcontrol_new cs35l32_snd_controls[] = {
118 SOC_SINGLE_TLV("Speaker Volume", CS35L32_CLASSD_CTL,
119 3, 0x04, 1, classd_ctl_tlv),
120 SOC_SINGLE("Zero Cross Switch", CS35L32_CLASSD_CTL, 2, 1, 0),
121 SOC_SINGLE("Gain Manager Switch", CS35L32_AUDIO_LED_MNGR, 3, 1, 0),
122};
123
124static const struct snd_soc_dapm_widget cs35l32_dapm_widgets[] = {
125
126 SND_SOC_DAPM_SUPPLY("BOOST", CS35L32_PWRCTL1, 2, 1, NULL, 0),
127 SND_SOC_DAPM_OUT_DRV("Speaker", CS35L32_PWRCTL1, 7, 1, NULL, 0),
128
129 SND_SOC_DAPM_AIF_OUT("SDOUT", NULL, 0, CS35L32_PWRCTL2, 3, 1),
130
131 SND_SOC_DAPM_INPUT("VP"),
132 SND_SOC_DAPM_INPUT("ISENSE"),
133 SND_SOC_DAPM_INPUT("VSENSE"),
134
135 SND_SOC_DAPM_SWITCH("VMON ADC", CS35L32_PWRCTL2, 7, 1, &vmon_ctl),
136 SND_SOC_DAPM_SWITCH("IMON ADC", CS35L32_PWRCTL2, 6, 1, &imon_ctl),
137 SND_SOC_DAPM_SWITCH("VPMON ADC", CS35L32_PWRCTL2, 5, 1, &vpmon_ctl),
138};
139
140static const struct snd_soc_dapm_route cs35l32_audio_map[] = {
141
142 {"Speaker", NULL, "BOOST"},
143
144 {"VMON ADC", NULL, "VSENSE"},
145 {"IMON ADC", NULL, "ISENSE"},
146 {"VPMON ADC", NULL, "VP"},
147
148 {"SDOUT", "Switch", "VMON ADC"},
149 {"SDOUT", "Switch", "IMON ADC"},
150 {"SDOUT", "Switch", "VPMON ADC"},
151
152 {"Capture", NULL, "SDOUT"},
153};
154
155static int cs35l32_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
156{
157 struct snd_soc_component *component = codec_dai->component;
158
159 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
160 case SND_SOC_DAIFMT_CBM_CFM:
161 snd_soc_component_update_bits(component, CS35L32_ADSP_CTL,
162 CS35L32_ADSP_MASTER_MASK,
163 CS35L32_ADSP_MASTER_MASK);
164 break;
165 case SND_SOC_DAIFMT_CBS_CFS:
166 snd_soc_component_update_bits(component, CS35L32_ADSP_CTL,
167 CS35L32_ADSP_MASTER_MASK, 0);
168 break;
169 default:
170 return -EINVAL;
171 }
172
173 return 0;
174}
175
176static int cs35l32_set_tristate(struct snd_soc_dai *dai, int tristate)
177{
178 struct snd_soc_component *component = dai->component;
179
180 return snd_soc_component_update_bits(component, CS35L32_PWRCTL2,
181 CS35L32_SDOUT_3ST, tristate << 3);
182}
183
184static const struct snd_soc_dai_ops cs35l32_ops = {
185 .set_fmt = cs35l32_set_dai_fmt,
186 .set_tristate = cs35l32_set_tristate,
187};
188
189static struct snd_soc_dai_driver cs35l32_dai[] = {
190 {
191 .name = "cs35l32-monitor",
192 .id = 0,
193 .capture = {
194 .stream_name = "Capture",
195 .channels_min = 2,
196 .channels_max = 2,
197 .rates = CS35L32_RATES,
198 .formats = CS35L32_FORMATS,
199 },
200 .ops = &cs35l32_ops,
201 .symmetric_rates = 1,
202 }
203};
204
205static int cs35l32_component_set_sysclk(struct snd_soc_component *component,
206 int clk_id, int source, unsigned int freq, int dir)
207{
208 unsigned int val;
209
210 switch (freq) {
211 case 6000000:
212 val = CS35L32_MCLK_RATIO;
213 break;
214 case 12000000:
215 val = CS35L32_MCLK_DIV2_MASK | CS35L32_MCLK_RATIO;
216 break;
217 case 6144000:
218 val = 0;
219 break;
220 case 12288000:
221 val = CS35L32_MCLK_DIV2_MASK;
222 break;
223 default:
224 return -EINVAL;
225 }
226
227 return snd_soc_component_update_bits(component, CS35L32_CLK_CTL,
228 CS35L32_MCLK_DIV2_MASK | CS35L32_MCLK_RATIO_MASK, val);
229}
230
231static const struct snd_soc_component_driver soc_component_dev_cs35l32 = {
232 .set_sysclk = cs35l32_component_set_sysclk,
233 .controls = cs35l32_snd_controls,
234 .num_controls = ARRAY_SIZE(cs35l32_snd_controls),
235 .dapm_widgets = cs35l32_dapm_widgets,
236 .num_dapm_widgets = ARRAY_SIZE(cs35l32_dapm_widgets),
237 .dapm_routes = cs35l32_audio_map,
238 .num_dapm_routes = ARRAY_SIZE(cs35l32_audio_map),
239 .idle_bias_on = 1,
240 .use_pmdown_time = 1,
241 .endianness = 1,
242 .non_legacy_dai_naming = 1,
243};
244
245
246static const struct reg_sequence cs35l32_monitor_patch[] = {
247
248 { 0x00, 0x99 },
249 { 0x48, 0x17 },
250 { 0x49, 0x56 },
251 { 0x43, 0x01 },
252 { 0x3B, 0x62 },
253 { 0x3C, 0x80 },
254 { 0x00, 0x00 },
255};
256
257static const struct regmap_config cs35l32_regmap = {
258 .reg_bits = 8,
259 .val_bits = 8,
260
261 .max_register = CS35L32_MAX_REGISTER,
262 .reg_defaults = cs35l32_reg_defaults,
263 .num_reg_defaults = ARRAY_SIZE(cs35l32_reg_defaults),
264 .volatile_reg = cs35l32_volatile_register,
265 .readable_reg = cs35l32_readable_register,
266 .precious_reg = cs35l32_precious_register,
267 .cache_type = REGCACHE_RBTREE,
268};
269
270static int cs35l32_handle_of_data(struct i2c_client *i2c_client,
271 struct cs35l32_platform_data *pdata)
272{
273 struct device_node *np = i2c_client->dev.of_node;
274 unsigned int val;
275
276 if (of_property_read_u32(np, "cirrus,sdout-share", &val) >= 0)
277 pdata->sdout_share = val;
278
279 if (of_property_read_u32(np, "cirrus,boost-manager", &val))
280 val = -1u;
281
282 switch (val) {
283 case CS35L32_BOOST_MGR_AUTO:
284 case CS35L32_BOOST_MGR_AUTO_AUDIO:
285 case CS35L32_BOOST_MGR_BYPASS:
286 case CS35L32_BOOST_MGR_FIXED:
287 pdata->boost_mng = val;
288 break;
289 case -1u:
290 default:
291 dev_err(&i2c_client->dev,
292 "Wrong cirrus,boost-manager DT value %d\n", val);
293 pdata->boost_mng = CS35L32_BOOST_MGR_BYPASS;
294 }
295
296 if (of_property_read_u32(np, "cirrus,sdout-datacfg", &val))
297 val = -1u;
298 switch (val) {
299 case CS35L32_DATA_CFG_LR_VP:
300 case CS35L32_DATA_CFG_LR_STAT:
301 case CS35L32_DATA_CFG_LR:
302 case CS35L32_DATA_CFG_LR_VPSTAT:
303 pdata->sdout_datacfg = val;
304 break;
305 case -1u:
306 default:
307 dev_err(&i2c_client->dev,
308 "Wrong cirrus,sdout-datacfg DT value %d\n", val);
309 pdata->sdout_datacfg = CS35L32_DATA_CFG_LR;
310 }
311
312 if (of_property_read_u32(np, "cirrus,battery-threshold", &val))
313 val = -1u;
314 switch (val) {
315 case CS35L32_BATT_THRESH_3_1V:
316 case CS35L32_BATT_THRESH_3_2V:
317 case CS35L32_BATT_THRESH_3_3V:
318 case CS35L32_BATT_THRESH_3_4V:
319 pdata->batt_thresh = val;
320 break;
321 case -1u:
322 default:
323 dev_err(&i2c_client->dev,
324 "Wrong cirrus,battery-threshold DT value %d\n", val);
325 pdata->batt_thresh = CS35L32_BATT_THRESH_3_3V;
326 }
327
328 if (of_property_read_u32(np, "cirrus,battery-recovery", &val))
329 val = -1u;
330 switch (val) {
331 case CS35L32_BATT_RECOV_3_1V:
332 case CS35L32_BATT_RECOV_3_2V:
333 case CS35L32_BATT_RECOV_3_3V:
334 case CS35L32_BATT_RECOV_3_4V:
335 case CS35L32_BATT_RECOV_3_5V:
336 case CS35L32_BATT_RECOV_3_6V:
337 pdata->batt_recov = val;
338 break;
339 case -1u:
340 default:
341 dev_err(&i2c_client->dev,
342 "Wrong cirrus,battery-recovery DT value %d\n", val);
343 pdata->batt_recov = CS35L32_BATT_RECOV_3_4V;
344 }
345
346 return 0;
347}
348
349static int cs35l32_i2c_probe(struct i2c_client *i2c_client,
350 const struct i2c_device_id *id)
351{
352 struct cs35l32_private *cs35l32;
353 struct cs35l32_platform_data *pdata =
354 dev_get_platdata(&i2c_client->dev);
355 int ret, i;
356 unsigned int devid = 0;
357 unsigned int reg;
358
359 cs35l32 = devm_kzalloc(&i2c_client->dev, sizeof(*cs35l32), GFP_KERNEL);
360 if (!cs35l32)
361 return -ENOMEM;
362
363 i2c_set_clientdata(i2c_client, cs35l32);
364
365 cs35l32->regmap = devm_regmap_init_i2c(i2c_client, &cs35l32_regmap);
366 if (IS_ERR(cs35l32->regmap)) {
367 ret = PTR_ERR(cs35l32->regmap);
368 dev_err(&i2c_client->dev, "regmap_init() failed: %d\n", ret);
369 return ret;
370 }
371
372 if (pdata) {
373 cs35l32->pdata = *pdata;
374 } else {
375 pdata = devm_kzalloc(&i2c_client->dev, sizeof(*pdata),
376 GFP_KERNEL);
377 if (!pdata)
378 return -ENOMEM;
379
380 if (i2c_client->dev.of_node) {
381 ret = cs35l32_handle_of_data(i2c_client,
382 &cs35l32->pdata);
383 if (ret != 0)
384 return ret;
385 }
386 }
387
388 for (i = 0; i < ARRAY_SIZE(cs35l32->supplies); i++)
389 cs35l32->supplies[i].supply = cs35l32_supply_names[i];
390
391 ret = devm_regulator_bulk_get(&i2c_client->dev,
392 ARRAY_SIZE(cs35l32->supplies),
393 cs35l32->supplies);
394 if (ret != 0) {
395 dev_err(&i2c_client->dev,
396 "Failed to request supplies: %d\n", ret);
397 return ret;
398 }
399
400 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l32->supplies),
401 cs35l32->supplies);
402 if (ret != 0) {
403 dev_err(&i2c_client->dev,
404 "Failed to enable supplies: %d\n", ret);
405 return ret;
406 }
407
408
409 cs35l32->reset_gpio = devm_gpiod_get_optional(&i2c_client->dev,
410 "reset", GPIOD_OUT_LOW);
411 if (IS_ERR(cs35l32->reset_gpio))
412 return PTR_ERR(cs35l32->reset_gpio);
413
414 gpiod_set_value_cansleep(cs35l32->reset_gpio, 1);
415
416
417 ret = regmap_read(cs35l32->regmap, CS35L32_DEVID_AB, ®);
418 devid = (reg & 0xFF) << 12;
419
420 ret = regmap_read(cs35l32->regmap, CS35L32_DEVID_CD, ®);
421 devid |= (reg & 0xFF) << 4;
422
423 ret = regmap_read(cs35l32->regmap, CS35L32_DEVID_E, ®);
424 devid |= (reg & 0xF0) >> 4;
425
426 if (devid != CS35L32_CHIP_ID) {
427 ret = -ENODEV;
428 dev_err(&i2c_client->dev,
429 "CS35L32 Device ID (%X). Expected %X\n",
430 devid, CS35L32_CHIP_ID);
431 return ret;
432 }
433
434 ret = regmap_read(cs35l32->regmap, CS35L32_REV_ID, ®);
435 if (ret < 0) {
436 dev_err(&i2c_client->dev, "Get Revision ID failed\n");
437 return ret;
438 }
439
440 ret = regmap_register_patch(cs35l32->regmap, cs35l32_monitor_patch,
441 ARRAY_SIZE(cs35l32_monitor_patch));
442 if (ret < 0) {
443 dev_err(&i2c_client->dev, "Failed to apply errata patch\n");
444 return ret;
445 }
446
447 dev_info(&i2c_client->dev,
448 "Cirrus Logic CS35L32, Revision: %02X\n", reg & 0xFF);
449
450
451 if (cs35l32->pdata.boost_mng)
452 regmap_update_bits(cs35l32->regmap, CS35L32_AUDIO_LED_MNGR,
453 CS35L32_BOOST_MASK,
454 cs35l32->pdata.boost_mng);
455
456
457 if (cs35l32->pdata.sdout_share)
458 regmap_update_bits(cs35l32->regmap, CS35L32_ADSP_CTL,
459 CS35L32_ADSP_SHARE_MASK,
460 cs35l32->pdata.sdout_share << 3);
461
462
463 if (cs35l32->pdata.sdout_datacfg)
464 regmap_update_bits(cs35l32->regmap, CS35L32_ADSP_CTL,
465 CS35L32_ADSP_DATACFG_MASK,
466 cs35l32->pdata.sdout_datacfg << 4);
467
468
469 if (cs35l32->pdata.batt_recov)
470 regmap_update_bits(cs35l32->regmap, CS35L32_BATT_THRESHOLD,
471 CS35L32_BATT_REC_MASK,
472 cs35l32->pdata.batt_recov << 1);
473
474
475 if (cs35l32->pdata.batt_thresh)
476 regmap_update_bits(cs35l32->regmap, CS35L32_BATT_THRESHOLD,
477 CS35L32_BATT_THRESH_MASK,
478 cs35l32->pdata.batt_thresh << 4);
479
480
481 regmap_update_bits(cs35l32->regmap, CS35L32_PWRCTL1, CS35L32_PDN_AMP,
482 CS35L32_PDN_AMP);
483
484
485 ret = regmap_read(cs35l32->regmap, CS35L32_INT_STATUS_1, ®);
486
487 ret = devm_snd_soc_register_component(&i2c_client->dev,
488 &soc_component_dev_cs35l32, cs35l32_dai,
489 ARRAY_SIZE(cs35l32_dai));
490 if (ret < 0)
491 goto err_disable;
492
493 return 0;
494
495err_disable:
496 regulator_bulk_disable(ARRAY_SIZE(cs35l32->supplies),
497 cs35l32->supplies);
498 return ret;
499}
500
501static int cs35l32_i2c_remove(struct i2c_client *i2c_client)
502{
503 struct cs35l32_private *cs35l32 = i2c_get_clientdata(i2c_client);
504
505
506 gpiod_set_value_cansleep(cs35l32->reset_gpio, 0);
507
508 return 0;
509}
510
511#ifdef CONFIG_PM
512static int cs35l32_runtime_suspend(struct device *dev)
513{
514 struct cs35l32_private *cs35l32 = dev_get_drvdata(dev);
515
516 regcache_cache_only(cs35l32->regmap, true);
517 regcache_mark_dirty(cs35l32->regmap);
518
519
520 gpiod_set_value_cansleep(cs35l32->reset_gpio, 0);
521
522
523 regulator_bulk_disable(ARRAY_SIZE(cs35l32->supplies),
524 cs35l32->supplies);
525
526 return 0;
527}
528
529static int cs35l32_runtime_resume(struct device *dev)
530{
531 struct cs35l32_private *cs35l32 = dev_get_drvdata(dev);
532 int ret;
533
534
535 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l32->supplies),
536 cs35l32->supplies);
537 if (ret != 0) {
538 dev_err(dev, "Failed to enable supplies: %d\n",
539 ret);
540 return ret;
541 }
542
543 gpiod_set_value_cansleep(cs35l32->reset_gpio, 1);
544
545 regcache_cache_only(cs35l32->regmap, false);
546 regcache_sync(cs35l32->regmap);
547
548 return 0;
549}
550#endif
551
552static const struct dev_pm_ops cs35l32_runtime_pm = {
553 SET_RUNTIME_PM_OPS(cs35l32_runtime_suspend, cs35l32_runtime_resume,
554 NULL)
555};
556
557static const struct of_device_id cs35l32_of_match[] = {
558 { .compatible = "cirrus,cs35l32", },
559 {},
560};
561MODULE_DEVICE_TABLE(of, cs35l32_of_match);
562
563
564static const struct i2c_device_id cs35l32_id[] = {
565 {"cs35l32", 0},
566 {}
567};
568
569MODULE_DEVICE_TABLE(i2c, cs35l32_id);
570
571static struct i2c_driver cs35l32_i2c_driver = {
572 .driver = {
573 .name = "cs35l32",
574 .pm = &cs35l32_runtime_pm,
575 .of_match_table = cs35l32_of_match,
576 },
577 .id_table = cs35l32_id,
578 .probe = cs35l32_i2c_probe,
579 .remove = cs35l32_i2c_remove,
580};
581
582module_i2c_driver(cs35l32_i2c_driver);
583
584MODULE_DESCRIPTION("ASoC CS35L32 driver");
585MODULE_AUTHOR("Brian Austin, Cirrus Logic Inc, <brian.austin@cirrus.com>");
586MODULE_LICENSE("GPL");
587