summaryrefslogtreecommitdiff
path: root/sound/pci/hda/cs35l41_hda.c
blob: 30b40d865863f0a3c2b54079d70df8e5f38317df (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
//
// cs35l41.c -- CS35l41 ALSA HDA audio driver
//
// Copyright 2021 Cirrus Logic, Inc.
//
// Author: Lucas Tanure <tanureal@opensource.cirrus.com>

#include <linux/acpi.h>
#include <linux/module.h>
#include <sound/hda_codec.h>
#include "hda_local.h"
#include "hda_auto_parser.h"
#include "hda_jack.h"
#include "hda_generic.h"
#include "hda_component.h"
#include "cs35l41_hda.h"

static const struct reg_sequence cs35l41_hda_config[] = {
	{ CS35L41_PLL_CLK_CTRL,		0x00000430 }, //3200000Hz, BCLK Input, PLL_REFCLK_EN = 1
	{ CS35L41_GLOBAL_CLK_CTRL,	0x00000003 }, //GLOBAL_FS = 48 kHz
	{ CS35L41_SP_ENABLES,		0x00010000 }, //ASP_RX1_EN = 1
	{ CS35L41_SP_RATE_CTRL,		0x00000021 }, //ASP_BCLK_FREQ = 3.072 MHz
	{ CS35L41_SP_FORMAT,		0x20200200 }, //24 bits, I2S, BCLK Slave, FSYNC Slave
	{ CS35L41_DAC_PCM1_SRC,		0x00000008 }, //DACPCM1_SRC = ASPRX1
	{ CS35L41_AMP_DIG_VOL_CTRL,	0x00000000 }, //AMP_VOL_PCM  0.0 dB
	{ CS35L41_AMP_GAIN_CTRL,	0x00000084 }, //AMP_GAIN_PCM 4.5 dB
	{ CS35L41_PWR_CTRL2,		0x00000001 }, //AMP_EN = 1
};

static const struct reg_sequence cs35l41_hda_start_bst[] = {
	{ CS35L41_PWR_CTRL2,		0x00000021 }, //BST_EN = 10, AMP_EN = 1
	{ CS35L41_PWR_CTRL1,		0x00000001, 3000}, // set GLOBAL_EN = 1
};

static const struct reg_sequence cs35l41_hda_stop_bst[] = {
	{ CS35L41_PWR_CTRL1,		0x00000000, 3000}, // set GLOBAL_EN = 0
};

// only on amps where GPIO1 is used to control ext. VSPK switch
static const struct reg_sequence cs35l41_start_ext_vspk[] = {
	{ 0x00000040,			0x00000055 },
	{ 0x00000040,			0x000000AA },
	{ 0x00007438,			0x00585941 },
	{ 0x00007414,			0x08C82222 },
	{ 0x0000742C,			0x00000009 },
	{ 0x00011008,			0x00008001 },
	{ 0x0000742C,			0x0000000F },
	{ 0x0000742C,			0x00000079 },
	{ 0x00007438,			0x00585941 },
	{ CS35L41_PWR_CTRL1,		0x00000001, 3000}, // set GLOBAL_EN = 1
	{ 0x0000742C,			0x000000F9 },
	{ 0x00007438,			0x00580941 },
	{ 0x00000040,			0x000000CC },
	{ 0x00000040,			0x00000033 },
};

//only on amps where GPIO1 is used to control ext. VSPK switch
static const struct reg_sequence cs35l41_stop_ext_vspk[] = {
	{ 0x00000040,			0x00000055 },
	{ 0x00000040,			0x000000AA },
	{ 0x00007438,			0x00585941 },
	{ 0x00002014,			0x00000000, 3000}, //set GLOBAL_EN = 0
	{ 0x0000742C,			0x00000009 },
	{ 0x00007438,			0x00580941 },
	{ 0x00011008,			0x00000001 },
	{ 0x0000393C,			0x000000C0, 6000},
	{ 0x0000393C,			0x00000000 },
	{ 0x00007414,			0x00C82222 },
	{ 0x0000742C,			0x00000000 },
	{ 0x00000040,			0x000000CC },
	{ 0x00000040,			0x00000033 },
};

static const struct reg_sequence cs35l41_safe_to_active[] = {
	{ 0x00000040,			0x00000055 },
	{ 0x00000040,			0x000000AA },
	{ 0x0000742C,			0x0000000F },
	{ 0x0000742C,			0x00000079 },
	{ 0x00007438,			0x00585941 },
	{ CS35L41_PWR_CTRL1,		0x00000001, 2000 }, //GLOBAL_EN = 1
	{ 0x0000742C,			0x000000F9 },
	{ 0x00007438,			0x00580941 },
	{ 0x00000040,			0x000000CC },
	{ 0x00000040,			0x00000033 },
};

static const struct reg_sequence cs35l41_active_to_safe[] = {
	{ 0x00000040,			0x00000055 },
	{ 0x00000040,			0x000000AA },
	{ 0x00007438,			0x00585941 },
	{ CS35L41_AMP_DIG_VOL_CTRL,	0x0000A678 }, //AMP_VOL_PCM Mute
	{ CS35L41_PWR_CTRL2,		0x00000000 }, //AMP_EN = 0
	{ CS35L41_PWR_CTRL1,		0x00000000 },
	{ 0x0000742C,			0x00000009, 2000 },
	{ 0x00007438,			0x00580941 },
	{ 0x00000040,			0x000000CC },
	{ 0x00000040,			0x00000033 },
};

static const struct reg_sequence cs35l41_reset_to_safe[] = {
	{ 0x00000040,			0x00000055 },
	{ 0x00000040,			0x000000AA },
	{ 0x00007438,			0x00585941 },
	{ 0x00007414,			0x08C82222 },
	{ 0x0000742C,			0x00000009 },
	{ 0x00000040,			0x000000CC },
	{ 0x00000040,			0x00000033 },
};

static const struct cs35l41_hda_reg_sequence cs35l41_hda_reg_seq_no_bst = {
	.probe		= cs35l41_reset_to_safe,
	.num_probe	= ARRAY_SIZE(cs35l41_reset_to_safe),
	.open		= cs35l41_hda_config,
	.num_open	= ARRAY_SIZE(cs35l41_hda_config),
	.prepare	= cs35l41_safe_to_active,
	.num_prepare	= ARRAY_SIZE(cs35l41_safe_to_active),
	.cleanup	= cs35l41_active_to_safe,
	.num_cleanup	= ARRAY_SIZE(cs35l41_active_to_safe),
};

static const struct cs35l41_hda_reg_sequence cs35l41_hda_reg_seq_ext_bst = {
	.open		= cs35l41_hda_config,
	.num_open	= ARRAY_SIZE(cs35l41_hda_config),
	.prepare	= cs35l41_start_ext_vspk,
	.num_prepare	= ARRAY_SIZE(cs35l41_start_ext_vspk),
	.cleanup	= cs35l41_stop_ext_vspk,
	.num_cleanup	= ARRAY_SIZE(cs35l41_stop_ext_vspk),
};

static const struct cs35l41_hda_reg_sequence cs35l41_hda_reg_seq_int_bst = {
	.open		= cs35l41_hda_config,
	.num_open	= ARRAY_SIZE(cs35l41_hda_config),
	.prepare	= cs35l41_hda_start_bst,
	.num_prepare	= ARRAY_SIZE(cs35l41_hda_start_bst),
	.cleanup	= cs35l41_hda_stop_bst,
	.num_cleanup	= ARRAY_SIZE(cs35l41_hda_stop_bst),
};

static void cs35l41_hda_playback_hook(struct device *dev, int action)
{
	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
	const struct cs35l41_hda_reg_sequence *reg_seq = cs35l41->reg_seq;
	struct regmap *reg = cs35l41->regmap;
	int ret = 0;

	switch (action) {
	case HDA_GEN_PCM_ACT_OPEN:
		if (reg_seq->open)
			ret = regmap_multi_reg_write(reg, reg_seq->open, reg_seq->num_open);
		break;
	case HDA_GEN_PCM_ACT_PREPARE:
		if (reg_seq->prepare)
			ret = regmap_multi_reg_write(reg, reg_seq->prepare, reg_seq->num_prepare);
		break;
	case HDA_GEN_PCM_ACT_CLEANUP:
		if (reg_seq->cleanup)
			ret = regmap_multi_reg_write(reg, reg_seq->cleanup, reg_seq->num_cleanup);
		break;
	case HDA_GEN_PCM_ACT_CLOSE:
		if (reg_seq->close)
			ret = regmap_multi_reg_write(reg, reg_seq->close, reg_seq->num_close);
		break;
	}

	if (ret)
		dev_warn(cs35l41->dev, "Failed to apply multi reg write: %d\n", ret);

}

static int cs35l41_hda_channel_map(struct device *dev, unsigned int tx_num, unsigned int *tx_slot,
				    unsigned int rx_num, unsigned int *rx_slot)
{
	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);

	return cs35l41_set_channels(cs35l41->dev, cs35l41->regmap, tx_num, tx_slot, rx_num,
				    rx_slot);
}

static int cs35l41_hda_bind(struct device *dev, struct device *master, void *master_data)
{
	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
	struct hda_component *comps = master_data;

	if (comps && cs35l41->index >= 0 && cs35l41->index < HDA_MAX_COMPONENTS)
		comps = &comps[cs35l41->index];
	else
		return -EINVAL;

	if (!comps->dev) {
		comps->dev = dev;
		strscpy(comps->name, dev_name(dev), sizeof(comps->name));
		comps->playback_hook = cs35l41_hda_playback_hook;
		comps->set_channel_map = cs35l41_hda_channel_map;
		return 0;
	}

	return -EBUSY;
}

static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *master_data)
{
	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
	struct hda_component *comps = master_data;

	if (comps[cs35l41->index].dev == dev)
		memset(&comps[cs35l41->index], 0, sizeof(*comps));
}

static const struct component_ops cs35l41_hda_comp_ops = {
	.bind = cs35l41_hda_bind,
	.unbind = cs35l41_hda_unbind,
};

static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41,
					const struct cs35l41_hda_hw_config *hw_cfg)
{
	bool internal_boost = false;
	int ret;

	if (!hw_cfg) {
		cs35l41->reg_seq = &cs35l41_hda_reg_seq_no_bst;
		return 0;
	}

	if (hw_cfg->bst_ind || hw_cfg->bst_cap || hw_cfg->bst_ipk)
		internal_boost = true;

	switch (hw_cfg->gpio1_func) {
	case CS35l41_VSPK_SWITCH:
		regmap_update_bits(cs35l41->regmap, CS35L41_GPIO_PAD_CONTROL,
				   CS35L41_GPIO1_CTRL_MASK, 1 << CS35L41_GPIO1_CTRL_SHIFT);
		break;
	case CS35l41_SYNC:
		regmap_update_bits(cs35l41->regmap, CS35L41_GPIO_PAD_CONTROL,
				   CS35L41_GPIO1_CTRL_MASK, 2 << CS35L41_GPIO1_CTRL_SHIFT);
		break;
	}

	switch (hw_cfg->gpio2_func) {
	case CS35L41_INTERRUPT:
		regmap_update_bits(cs35l41->regmap, CS35L41_GPIO_PAD_CONTROL,
				   CS35L41_GPIO2_CTRL_MASK, 2 << CS35L41_GPIO2_CTRL_SHIFT);
		break;
	}

	if (internal_boost) {
		cs35l41->reg_seq = &cs35l41_hda_reg_seq_int_bst;
		if (!(hw_cfg->bst_ind && hw_cfg->bst_cap && hw_cfg->bst_ipk))
			return -EINVAL;
		ret = cs35l41_boost_config(cs35l41->dev, cs35l41->regmap,
					   hw_cfg->bst_ind, hw_cfg->bst_cap, hw_cfg->bst_ipk);
		if (ret)
			return ret;
	} else {
		cs35l41->reg_seq = &cs35l41_hda_reg_seq_ext_bst;
	}

	ret = cs35l41_hda_channel_map(cs35l41->dev, 0, NULL, 1, (unsigned int *)&hw_cfg->spk_pos);
	if (ret)
		return ret;

	return 0;
}

static struct cs35l41_hda_hw_config *cs35l41_hda_read_acpi(struct cs35l41_hda *cs35l41,
							   const char *hid, int id)
{
	struct cs35l41_hda_hw_config *hw_cfg;
	u32 values[HDA_MAX_COMPONENTS];
	struct acpi_device *adev;
	struct device *acpi_dev;
	char *property;
	size_t nval;
	int i, ret;

	adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
	if (!adev) {
		dev_err(cs35l41->dev, "Failed to find an ACPI device for %s\n", hid);
		return ERR_PTR(-ENODEV);
	}

	acpi_dev = get_device(acpi_get_first_physical_node(adev));
	acpi_dev_put(adev);

	property = "cirrus,dev-index";
	ret = device_property_count_u32(acpi_dev, property);
	if (ret <= 0)
		goto no_acpi_dsd;

	if (ret > ARRAY_SIZE(values)) {
		ret = -EINVAL;
		goto err;
	}
	nval = ret;

	ret = device_property_read_u32_array(acpi_dev, property, values, nval);
	if (ret)
		goto err;

	cs35l41->index = -1;
	for (i = 0; i < nval; i++) {
		if (values[i] == id) {
			cs35l41->index = i;
			break;
		}
	}
	if (cs35l41->index == -1) {
		dev_err(cs35l41->dev, "No index found in %s\n", property);
		ret = -ENODEV;
		goto err;
	}

	/* No devm_ version as CLSA0100, in no_acpi_dsd case, can't use devm version */
	cs35l41->reset_gpio = fwnode_gpiod_get_index(&adev->fwnode, "reset", cs35l41->index,
						     GPIOD_OUT_LOW, "cs35l41-reset");

	hw_cfg = kzalloc(sizeof(*hw_cfg), GFP_KERNEL);
	if (!hw_cfg) {
		ret = -ENOMEM;
		goto err;
	}

	property = "cirrus,speaker-position";
	ret = device_property_read_u32_array(acpi_dev, property, values, nval);
	if (ret)
		goto err_free;
	hw_cfg->spk_pos = values[cs35l41->index];

	property = "cirrus,gpio1-func";
	ret = device_property_read_u32_array(acpi_dev, property, values, nval);
	if (ret)
		goto err_free;
	hw_cfg->gpio1_func = values[cs35l41->index];

	property = "cirrus,gpio2-func";
	ret = device_property_read_u32_array(acpi_dev, property, values, nval);
	if (ret)
		goto err_free;
	hw_cfg->gpio2_func = values[cs35l41->index];

	property = "cirrus,boost-peak-milliamp";
	ret = device_property_read_u32_array(acpi_dev, property, values, nval);
	if (ret == 0)
		hw_cfg->bst_ipk = values[cs35l41->index];

	property = "cirrus,boost-ind-nanohenry";
	ret = device_property_read_u32_array(acpi_dev, property, values, nval);
	if (ret == 0)
		hw_cfg->bst_ind = values[cs35l41->index];

	property = "cirrus,boost-cap-microfarad";
	ret = device_property_read_u32_array(acpi_dev, property, values, nval);
	if (ret == 0)
		hw_cfg->bst_cap = values[cs35l41->index];

	put_device(acpi_dev);

	return hw_cfg;

err_free:
	kfree(hw_cfg);
err:
	put_device(acpi_dev);
	dev_err(cs35l41->dev, "Failed property %s: %d\n", property, ret);

	return ERR_PTR(ret);

no_acpi_dsd:
	/*
	 * Device CLSA0100 doesn't have _DSD so a gpiod_get by the label reset won't work.
	 * And devices created by i2c-multi-instantiate don't have their device struct pointing to
	 * the correct fwnode, so acpi_dev must be used here
	 * And devm functions expect that the device requesting the resource has the correct
	 * fwnode
	 */
	if (strncmp(hid, "CLSA0100", 8) != 0)
		return ERR_PTR(-EINVAL);

	/* check I2C address to assign the index */
	cs35l41->index = id == 0x40 ? 0 : 1;
	cs35l41->reset_gpio = gpiod_get_index(acpi_dev, NULL, 0, GPIOD_OUT_HIGH);
	cs35l41->vspk_always_on = true;
	put_device(acpi_dev);

	return NULL;
}

int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq,
		      struct regmap *regmap)
{
	unsigned int int_sts, regid, reg_revid, mtl_revid, chipid, int_status;
	struct cs35l41_hda_hw_config *acpi_hw_cfg;
	struct cs35l41_hda *cs35l41;
	int ret;

	if (IS_ERR(regmap))
		return PTR_ERR(regmap);

	cs35l41 = devm_kzalloc(dev, sizeof(*cs35l41), GFP_KERNEL);
	if (!cs35l41)
		return -ENOMEM;

	cs35l41->dev = dev;
	cs35l41->irq = irq;
	cs35l41->regmap = regmap;
	dev_set_drvdata(dev, cs35l41);

	acpi_hw_cfg = cs35l41_hda_read_acpi(cs35l41, device_name, id);
	if (IS_ERR(acpi_hw_cfg))
		return PTR_ERR(acpi_hw_cfg);

	if (IS_ERR(cs35l41->reset_gpio)) {
		ret = PTR_ERR(cs35l41->reset_gpio);
		cs35l41->reset_gpio = NULL;
		if (ret == -EBUSY) {
			dev_info(cs35l41->dev, "Reset line busy, assuming shared reset\n");
		} else {
			if (ret != -EPROBE_DEFER)
				dev_err(cs35l41->dev, "Failed to get reset GPIO: %d\n", ret);
			goto err;
		}
	}
	if (cs35l41->reset_gpio) {
		usleep_range(2000, 2100);
		gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
	}

	usleep_range(2000, 2100);

	ret = regmap_read_poll_timeout(cs35l41->regmap, CS35L41_IRQ1_STATUS4, int_status,
				       int_status & CS35L41_OTP_BOOT_DONE, 1000, 100000);
	if (ret) {
		dev_err(cs35l41->dev, "Failed waiting for OTP_BOOT_DONE: %d\n", ret);
		goto err;
	}

	ret = regmap_read(cs35l41->regmap, CS35L41_IRQ1_STATUS3, &int_sts);
	if (ret || (int_sts & CS35L41_OTP_BOOT_ERR)) {
		dev_err(cs35l41->dev, "OTP Boot error\n");
		ret = -EIO;
		goto err;
	}

	ret = regmap_read(cs35l41->regmap, CS35L41_DEVID, &regid);
	if (ret) {
		dev_err(cs35l41->dev, "Get Device ID failed: %d\n", ret);
		goto err;
	}

	ret = regmap_read(cs35l41->regmap, CS35L41_REVID, &reg_revid);
	if (ret) {
		dev_err(cs35l41->dev, "Get Revision ID failed: %d\n", ret);
		goto err;
	}

	mtl_revid = reg_revid & CS35L41_MTLREVID_MASK;

	chipid = (mtl_revid % 2) ? CS35L41R_CHIP_ID : CS35L41_CHIP_ID;
	if (regid != chipid) {
		dev_err(cs35l41->dev, "CS35L41 Device ID (%X). Expected ID %X\n", regid, chipid);
		ret = -ENODEV;
		goto err;
	}

	ret = cs35l41_register_errata_patch(cs35l41->dev, cs35l41->regmap, reg_revid);
	if (ret)
		goto err;

	ret = cs35l41_otp_unpack(cs35l41->dev, cs35l41->regmap);
	if (ret) {
		dev_err(cs35l41->dev, "OTP Unpack failed: %d\n", ret);
		goto err;
	}

	ret = cs35l41_hda_apply_properties(cs35l41, acpi_hw_cfg);
	if (ret)
		goto err;
	kfree(acpi_hw_cfg);
	acpi_hw_cfg = NULL;

	if (cs35l41->reg_seq->probe) {
		ret = regmap_register_patch(cs35l41->regmap, cs35l41->reg_seq->probe,
					    cs35l41->reg_seq->num_probe);
		if (ret) {
			dev_err(cs35l41->dev, "Fail to apply probe reg patch: %d\n", ret);
			goto err;
		}
	}

	ret = component_add(cs35l41->dev, &cs35l41_hda_comp_ops);
	if (ret) {
		dev_err(cs35l41->dev, "Register component failed: %d\n", ret);
		goto err;
	}

	dev_info(cs35l41->dev, "Cirrus Logic CS35L41 (%x), Revision: %02X\n", regid, reg_revid);

	return 0;

err:
	kfree(acpi_hw_cfg);
	if (!cs35l41->vspk_always_on)
		gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
	gpiod_put(cs35l41->reset_gpio);

	return ret;
}
EXPORT_SYMBOL_GPL(cs35l41_hda_probe);

int cs35l41_hda_remove(struct device *dev)
{
	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);

	component_del(cs35l41->dev, &cs35l41_hda_comp_ops);

	if (!cs35l41->vspk_always_on)
		gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
	gpiod_put(cs35l41->reset_gpio);

	return 0;
}
EXPORT_SYMBOL_GPL(cs35l41_hda_remove);


MODULE_DESCRIPTION("CS35L41 HDA Driver");
MODULE_AUTHOR("Lucas Tanure, Cirrus Logic Inc, <tanureal@opensource.cirrus.com>");
MODULE_LICENSE("GPL");