xref: /openbmc/u-boot/drivers/sound/maxim_codec.c (revision e35171e9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * maxim_codec.c -- MAXIM CODEC Common driver
4  *
5  * Copyright 2011 Maxim Integrated Products
6  */
7 
8 #include <common.h>
9 #include <div64.h>
10 #include <i2c.h>
11 #include <i2s.h>
12 #include <sound.h>
13 #include <asm/gpio.h>
14 #include <asm/io.h>
15 #include "maxim_codec.h"
16 
17 /*
18  * Writes value to a device register through i2c
19  *
20  * @param priv	Private data for driver
21  * @param reg	reg number to be write
22  * @param data	data to be writen to the above registor
23  *
24  * @return	int value 1 for change, 0 for no change or negative error code.
25  */
maxim_i2c_write(struct maxim_priv * priv,unsigned int reg,unsigned char data)26 int maxim_i2c_write(struct maxim_priv *priv, unsigned int reg,
27 		    unsigned char data)
28 {
29 	debug("%s: Write Addr : 0x%02X, Data :  0x%02X\n",
30 	      __func__, reg, data);
31 	return dm_i2c_write(priv->dev, reg, &data, 1);
32 }
33 
34 /*
35  * Read a value from a device register through i2c
36  *
37  * @param priv	Private data for driver
38  * @param reg	reg number to be read
39  * @param data	address of read data to be stored
40  *
41  * @return	int value 0 for success, -1 in case of error.
42  */
maxim_i2c_read(struct maxim_priv * priv,unsigned int reg,unsigned char * data)43 unsigned int maxim_i2c_read(struct maxim_priv *priv, unsigned int reg,
44 			    unsigned char *data)
45 {
46 	int ret;
47 
48 	return dm_i2c_read(priv->dev, reg, data, 1);
49 	if (ret != 0) {
50 		debug("%s: Error while reading register %#04x\n",
51 		      __func__, reg);
52 		return -1;
53 	}
54 
55 	return 0;
56 }
57 
58 /*
59  * update device register bits through i2c
60  *
61  * @param priv	Private data for driver
62  * @param reg	codec register
63  * @param mask	register mask
64  * @param value	new value
65  *
66  * @return int value 0 for success, non-zero error code.
67  */
maxim_bic_or(struct maxim_priv * priv,unsigned int reg,unsigned char mask,unsigned char value)68 int maxim_bic_or(struct maxim_priv *priv, unsigned int reg, unsigned char mask,
69 		 unsigned char value)
70 {
71 	int change, ret = 0;
72 	unsigned char old, new;
73 
74 	if (maxim_i2c_read(priv, reg, &old) != 0)
75 		return -1;
76 	new = (old & ~mask) | (value & mask);
77 	change  = (old != new) ? 1 : 0;
78 	if (change)
79 		ret = maxim_i2c_write(priv, reg, new);
80 	if (ret < 0)
81 		return ret;
82 
83 	return change;
84 }
85