11// SPDX-License-Identifier: GPL-2.0
22//
3- // cs35l41.c -- CS35l41 ALSA HDA audio driver
3+ // CS35l41 ALSA HDA audio driver
44//
55// Copyright 2021 Cirrus Logic, Inc.
66//
1717#include "cs35l41_hda.h"
1818
1919static const struct reg_sequence cs35l41_hda_config [] = {
20- { CS35L41_PLL_CLK_CTRL , 0x00000430 }, //3200000Hz, BCLK Input, PLL_REFCLK_EN = 1
21- { CS35L41_GLOBAL_CLK_CTRL , 0x00000003 }, //GLOBAL_FS = 48 kHz
22- { CS35L41_SP_ENABLES , 0x00010000 }, //ASP_RX1_EN = 1
23- { CS35L41_SP_RATE_CTRL , 0x00000021 }, //ASP_BCLK_FREQ = 3.072 MHz
24- { CS35L41_SP_FORMAT , 0x20200200 }, //24 bits, I2S, BCLK Slave, FSYNC Slave
25- { CS35L41_DAC_PCM1_SRC , 0x00000008 }, //DACPCM1_SRC = ASPRX1
26- { CS35L41_AMP_DIG_VOL_CTRL , 0x00000000 }, //AMP_VOL_PCM 0.0 dB
27- { CS35L41_AMP_GAIN_CTRL , 0x00000084 }, //AMP_GAIN_PCM 4.5 dB
28- { CS35L41_PWR_CTRL2 , 0x00000001 }, //AMP_EN = 1
20+ { CS35L41_PLL_CLK_CTRL , 0x00000430 }, // 3200000Hz, BCLK Input, PLL_REFCLK_EN = 1
21+ { CS35L41_GLOBAL_CLK_CTRL , 0x00000003 }, // GLOBAL_FS = 48 kHz
22+ { CS35L41_SP_ENABLES , 0x00010000 }, // ASP_RX1_EN = 1
23+ { CS35L41_SP_RATE_CTRL , 0x00000021 }, // ASP_BCLK_FREQ = 3.072 MHz
24+ { CS35L41_SP_FORMAT , 0x20200200 }, // 24 bits, I2S, BCLK Slave, FSYNC Slave
25+ { CS35L41_DAC_PCM1_SRC , 0x00000008 }, // DACPCM1_SRC = ASPRX1
26+ { CS35L41_AMP_DIG_VOL_CTRL , 0x00000000 }, // AMP_VOL_PCM 0.0 dB
27+ { CS35L41_AMP_GAIN_CTRL , 0x00000084 }, // AMP_GAIN_PCM 4.5 dB
28+ { CS35L41_PWR_CTRL2 , 0x00000001 }, // AMP_EN = 1
2929};
3030
3131static const struct reg_sequence cs35l41_hda_start_bst [] = {
32- { CS35L41_PWR_CTRL2 , 0x00000021 }, //BST_EN = 10, AMP_EN = 1
32+ { CS35L41_PWR_CTRL2 , 0x00000021 }, // BST_EN = 10, AMP_EN = 1
3333 { CS35L41_PWR_CTRL1 , 0x00000001 , 3000 }, // set GLOBAL_EN = 1
3434};
3535
@@ -60,7 +60,7 @@ static const struct reg_sequence cs35l41_stop_ext_vspk[] = {
6060 { 0x00000040 , 0x00000055 },
6161 { 0x00000040 , 0x000000AA },
6262 { 0x00007438 , 0x00585941 },
63- { 0x00002014 , 0x00000000 , 3000 }, //set GLOBAL_EN = 0
63+ { 0x00002014 , 0x00000000 , 3000 }, // set GLOBAL_EN = 0
6464 { 0x0000742C , 0x00000009 },
6565 { 0x00007438 , 0x00580941 },
6666 { 0x00011008 , 0x00000001 },
@@ -78,7 +78,7 @@ static const struct reg_sequence cs35l41_safe_to_active[] = {
7878 { 0x0000742C , 0x0000000F },
7979 { 0x0000742C , 0x00000079 },
8080 { 0x00007438 , 0x00585941 },
81- { CS35L41_PWR_CTRL1 , 0x00000001 , 2000 }, //GLOBAL_EN = 1
81+ { CS35L41_PWR_CTRL1 , 0x00000001 , 2000 }, // GLOBAL_EN = 1
8282 { 0x0000742C , 0x000000F9 },
8383 { 0x00007438 , 0x00580941 },
8484 { 0x00000040 , 0x000000CC },
@@ -89,8 +89,8 @@ static const struct reg_sequence cs35l41_active_to_safe[] = {
8989 { 0x00000040 , 0x00000055 },
9090 { 0x00000040 , 0x000000AA },
9191 { 0x00007438 , 0x00585941 },
92- { CS35L41_AMP_DIG_VOL_CTRL , 0x0000A678 }, //AMP_VOL_PCM Mute
93- { CS35L41_PWR_CTRL2 , 0x00000000 }, //AMP_EN = 0
92+ { CS35L41_AMP_DIG_VOL_CTRL , 0x0000A678 }, // AMP_VOL_PCM Mute
93+ { CS35L41_PWR_CTRL2 , 0x00000000 }, // AMP_EN = 0
9494 { CS35L41_PWR_CTRL1 , 0x00000000 },
9595 { 0x0000742C , 0x00000009 , 2000 },
9696 { 0x00007438 , 0x00580941 },
@@ -161,11 +161,13 @@ static void cs35l41_hda_playback_hook(struct device *dev, int action)
161161 if (reg_seq -> close )
162162 ret = regmap_multi_reg_write (reg , reg_seq -> close , reg_seq -> num_close );
163163 break ;
164+ default :
165+ ret = - EINVAL ;
166+ break ;
164167 }
165168
166169 if (ret )
167170 dev_warn (cs35l41 -> dev , "Failed to apply multi reg write: %d\n" , ret );
168-
169171}
170172
171173static int cs35l41_hda_channel_map (struct device * dev , unsigned int tx_num , unsigned int * tx_slot ,
@@ -182,20 +184,19 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
182184 struct cs35l41_hda * cs35l41 = dev_get_drvdata (dev );
183185 struct hda_component * comps = master_data ;
184186
185- if (comps && cs35l41 -> index >= 0 && cs35l41 -> index < HDA_MAX_COMPONENTS )
186- comps = & comps [cs35l41 -> index ];
187- else
187+ if (!comps || cs35l41 -> index < 0 || cs35l41 -> index >= HDA_MAX_COMPONENTS )
188188 return - EINVAL ;
189189
190- if (!comps -> dev ) {
191- comps -> dev = dev ;
192- strscpy (comps -> name , dev_name (dev ), sizeof (comps -> name ));
193- comps -> playback_hook = cs35l41_hda_playback_hook ;
194- comps -> set_channel_map = cs35l41_hda_channel_map ;
195- return 0 ;
196- }
190+ comps = & comps [cs35l41 -> index ];
191+ if (comps -> dev )
192+ return - EBUSY ;
193+
194+ comps -> dev = dev ;
195+ strscpy (comps -> name , dev_name (dev ), sizeof (comps -> name ));
196+ comps -> playback_hook = cs35l41_hda_playback_hook ;
197+ comps -> set_channel_map = cs35l41_hda_channel_map ;
197198
198- return - EBUSY ;
199+ return 0 ;
199200}
200201
201202static void cs35l41_hda_unbind (struct device * dev , struct device * master , void * master_data )
@@ -227,6 +228,8 @@ static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41,
227228 internal_boost = true;
228229
229230 switch (hw_cfg -> gpio1_func ) {
231+ case CS35L41_NOT_USED :
232+ break ;
230233 case CS35l41_VSPK_SWITCH :
231234 regmap_update_bits (cs35l41 -> regmap , CS35L41_GPIO_PAD_CONTROL ,
232235 CS35L41_GPIO1_CTRL_MASK , 1 << CS35L41_GPIO1_CTRL_SHIFT );
@@ -235,13 +238,21 @@ static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41,
235238 regmap_update_bits (cs35l41 -> regmap , CS35L41_GPIO_PAD_CONTROL ,
236239 CS35L41_GPIO1_CTRL_MASK , 2 << CS35L41_GPIO1_CTRL_SHIFT );
237240 break ;
241+ default :
242+ dev_err (cs35l41 -> dev , "Invalid function %d for GPIO1\n" , hw_cfg -> gpio1_func );
243+ return - EINVAL ;
238244 }
239245
240246 switch (hw_cfg -> gpio2_func ) {
247+ case CS35L41_NOT_USED :
248+ break ;
241249 case CS35L41_INTERRUPT :
242250 regmap_update_bits (cs35l41 -> regmap , CS35L41_GPIO_PAD_CONTROL ,
243251 CS35L41_GPIO2_CTRL_MASK , 2 << CS35L41_GPIO2_CTRL_SHIFT );
244252 break ;
253+ default :
254+ dev_err (cs35l41 -> dev , "Invalid function %d for GPIO2\n" , hw_cfg -> gpio2_func );
255+ return - EINVAL ;
245256 }
246257
247258 if (internal_boost ) {
@@ -256,11 +267,7 @@ static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41,
256267 cs35l41 -> reg_seq = & cs35l41_hda_reg_seq_ext_bst ;
257268 }
258269
259- ret = cs35l41_hda_channel_map (cs35l41 -> dev , 0 , NULL , 1 , (unsigned int * )& hw_cfg -> spk_pos );
260- if (ret )
261- return ret ;
262-
263- return 0 ;
270+ return cs35l41_hda_channel_map (cs35l41 -> dev , 0 , NULL , 1 , (unsigned int * )& hw_cfg -> spk_pos );
264271}
265272
266273static struct cs35l41_hda_hw_config * cs35l41_hda_read_acpi (struct cs35l41_hda * cs35l41 ,
@@ -269,7 +276,7 @@ static struct cs35l41_hda_hw_config *cs35l41_hda_read_acpi(struct cs35l41_hda *c
269276 struct cs35l41_hda_hw_config * hw_cfg ;
270277 u32 values [HDA_MAX_COMPONENTS ];
271278 struct acpi_device * adev ;
272- struct device * acpi_dev ;
279+ struct device * physdev ;
273280 char * property ;
274281 size_t nval ;
275282 int i , ret ;
@@ -280,11 +287,11 @@ static struct cs35l41_hda_hw_config *cs35l41_hda_read_acpi(struct cs35l41_hda *c
280287 return ERR_PTR (- ENODEV );
281288 }
282289
283- acpi_dev = get_device (acpi_get_first_physical_node (adev ));
290+ physdev = get_device (acpi_get_first_physical_node (adev ));
284291 acpi_dev_put (adev );
285292
286293 property = "cirrus,dev-index" ;
287- ret = device_property_count_u32 (acpi_dev , property );
294+ ret = device_property_count_u32 (physdev , property );
288295 if (ret <= 0 )
289296 goto no_acpi_dsd ;
290297
@@ -294,7 +301,7 @@ static struct cs35l41_hda_hw_config *cs35l41_hda_read_acpi(struct cs35l41_hda *c
294301 }
295302 nval = ret ;
296303
297- ret = device_property_read_u32_array (acpi_dev , property , values , nval );
304+ ret = device_property_read_u32_array (physdev , property , values , nval );
298305 if (ret )
299306 goto err ;
300307
@@ -311,7 +318,9 @@ static struct cs35l41_hda_hw_config *cs35l41_hda_read_acpi(struct cs35l41_hda *c
311318 goto err ;
312319 }
313320
314- /* No devm_ version as CLSA0100, in no_acpi_dsd case, can't use devm version */
321+ /* To use the same release code for all laptop variants we can't use devm_ version of
322+ * gpiod_get here, as CLSA010* don't have a fully functional bios with an _DSD node
323+ */
315324 cs35l41 -> reset_gpio = fwnode_gpiod_get_index (& adev -> fwnode , "reset" , cs35l41 -> index ,
316325 GPIOD_OUT_LOW , "cs35l41-reset" );
317326
@@ -322,46 +331,46 @@ static struct cs35l41_hda_hw_config *cs35l41_hda_read_acpi(struct cs35l41_hda *c
322331 }
323332
324333 property = "cirrus,speaker-position" ;
325- ret = device_property_read_u32_array (acpi_dev , property , values , nval );
334+ ret = device_property_read_u32_array (physdev , property , values , nval );
326335 if (ret )
327336 goto err_free ;
328337 hw_cfg -> spk_pos = values [cs35l41 -> index ];
329338
330339 property = "cirrus,gpio1-func" ;
331- ret = device_property_read_u32_array (acpi_dev , property , values , nval );
340+ ret = device_property_read_u32_array (physdev , property , values , nval );
332341 if (ret )
333342 goto err_free ;
334343 hw_cfg -> gpio1_func = values [cs35l41 -> index ];
335344
336345 property = "cirrus,gpio2-func" ;
337- ret = device_property_read_u32_array (acpi_dev , property , values , nval );
346+ ret = device_property_read_u32_array (physdev , property , values , nval );
338347 if (ret )
339348 goto err_free ;
340349 hw_cfg -> gpio2_func = values [cs35l41 -> index ];
341350
342351 property = "cirrus,boost-peak-milliamp" ;
343- ret = device_property_read_u32_array (acpi_dev , property , values , nval );
352+ ret = device_property_read_u32_array (physdev , property , values , nval );
344353 if (ret == 0 )
345354 hw_cfg -> bst_ipk = values [cs35l41 -> index ];
346355
347356 property = "cirrus,boost-ind-nanohenry" ;
348- ret = device_property_read_u32_array (acpi_dev , property , values , nval );
357+ ret = device_property_read_u32_array (physdev , property , values , nval );
349358 if (ret == 0 )
350359 hw_cfg -> bst_ind = values [cs35l41 -> index ];
351360
352361 property = "cirrus,boost-cap-microfarad" ;
353- ret = device_property_read_u32_array (acpi_dev , property , values , nval );
362+ ret = device_property_read_u32_array (physdev , property , values , nval );
354363 if (ret == 0 )
355364 hw_cfg -> bst_cap = values [cs35l41 -> index ];
356365
357- put_device (acpi_dev );
366+ put_device (physdev );
358367
359368 return hw_cfg ;
360369
361370err_free :
362371 kfree (hw_cfg );
363372err :
364- put_device (acpi_dev );
373+ put_device (physdev );
365374 dev_err (cs35l41 -> dev , "Failed property %s: %d\n" , property , ret );
366375
367376 return ERR_PTR (ret );
@@ -370,18 +379,18 @@ static struct cs35l41_hda_hw_config *cs35l41_hda_read_acpi(struct cs35l41_hda *c
370379 /*
371380 * Device CLSA0100 doesn't have _DSD so a gpiod_get by the label reset won't work.
372381 * And devices created by i2c-multi-instantiate don't have their device struct pointing to
373- * the correct fwnode, so acpi_dev must be used here
382+ * the correct fwnode, so acpi_dev must be used here.
374383 * And devm functions expect that the device requesting the resource has the correct
375- * fwnode
384+ * fwnode.
376385 */
377386 if (strncmp (hid , "CLSA0100" , 8 ) != 0 )
378387 return ERR_PTR (- EINVAL );
379388
380389 /* check I2C address to assign the index */
381390 cs35l41 -> index = id == 0x40 ? 0 : 1 ;
382- cs35l41 -> reset_gpio = gpiod_get_index (acpi_dev , NULL , 0 , GPIOD_OUT_HIGH );
391+ cs35l41 -> reset_gpio = gpiod_get_index (physdev , NULL , 0 , GPIOD_OUT_HIGH );
383392 cs35l41 -> vspk_always_on = true;
384- put_device (acpi_dev );
393+ put_device (physdev );
385394
386395 return NULL ;
387396}
@@ -416,8 +425,7 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
416425 if (ret == - EBUSY ) {
417426 dev_info (cs35l41 -> dev , "Reset line busy, assuming shared reset\n" );
418427 } else {
419- if (ret != - EPROBE_DEFER )
420- dev_err (cs35l41 -> dev , "Failed to get reset GPIO: %d\n" , ret );
428+ dev_err_probe (cs35l41 -> dev , ret , "Failed to get reset GPIO: %d\n" , ret );
421429 goto err ;
422430 }
423431 }
@@ -437,7 +445,8 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
437445
438446 ret = regmap_read (cs35l41 -> regmap , CS35L41_IRQ1_STATUS3 , & int_sts );
439447 if (ret || (int_sts & CS35L41_OTP_BOOT_ERR )) {
440- dev_err (cs35l41 -> dev , "OTP Boot error\n" );
448+ dev_err (cs35l41 -> dev , "OTP Boot status %x error: %d\n" ,
449+ int_sts & CS35L41_OTP_BOOT_ERR , ret );
441450 ret = - EIO ;
442451 goto err ;
443452 }
@@ -463,6 +472,10 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
463472 goto err ;
464473 }
465474
475+ ret = cs35l41_test_key_unlock (cs35l41 -> dev , cs35l41 -> regmap );
476+ if (ret )
477+ goto err ;
478+
466479 ret = cs35l41_register_errata_patch (cs35l41 -> dev , cs35l41 -> regmap , reg_revid );
467480 if (ret )
468481 goto err ;
@@ -473,15 +486,19 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
473486 goto err ;
474487 }
475488
489+ ret = cs35l41_test_key_lock (cs35l41 -> dev , cs35l41 -> regmap );
490+ if (ret )
491+ goto err ;
492+
476493 ret = cs35l41_hda_apply_properties (cs35l41 , acpi_hw_cfg );
477494 if (ret )
478495 goto err ;
479496 kfree (acpi_hw_cfg );
480497 acpi_hw_cfg = NULL ;
481498
482499 if (cs35l41 -> reg_seq -> probe ) {
483- ret = regmap_register_patch (cs35l41 -> regmap , cs35l41 -> reg_seq -> probe ,
484- cs35l41 -> reg_seq -> num_probe );
500+ ret = regmap_multi_reg_write (cs35l41 -> regmap , cs35l41 -> reg_seq -> probe ,
501+ cs35l41 -> reg_seq -> num_probe );
485502 if (ret ) {
486503 dev_err (cs35l41 -> dev , "Fail to apply probe reg patch: %d\n" , ret );
487504 goto err ;
@@ -506,9 +523,9 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
506523
507524 return ret ;
508525}
509- EXPORT_SYMBOL_GPL (cs35l41_hda_probe );
526+ EXPORT_SYMBOL_NS_GPL (cs35l41_hda_probe , SND_HDA_SCODEC_CS35L41 );
510527
511- int cs35l41_hda_remove (struct device * dev )
528+ void cs35l41_hda_remove (struct device * dev )
512529{
513530 struct cs35l41_hda * cs35l41 = dev_get_drvdata (dev );
514531
@@ -517,11 +534,8 @@ int cs35l41_hda_remove(struct device *dev)
517534 if (!cs35l41 -> vspk_always_on )
518535 gpiod_set_value_cansleep (cs35l41 -> reset_gpio , 0 );
519536 gpiod_put (cs35l41 -> reset_gpio );
520-
521- return 0 ;
522537}
523- EXPORT_SYMBOL_GPL (cs35l41_hda_remove );
524-
538+ EXPORT_SYMBOL_NS_GPL (cs35l41_hda_remove , SND_HDA_SCODEC_CS35L41 );
525539
526540MODULE_DESCRIPTION ("CS35L41 HDA Driver" );
527541MODULE_AUTHOR ("Lucas Tanure, Cirrus Logic Inc, <tanureal@opensource.cirrus.com>" );
0 commit comments