blob: 6553d9590472b93b2dd3e33ce3afd924e84f886c [file] [log] [blame]
Simon Glass0ab6f0b2018-12-10 10:37:42 -07001// 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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass0ab6f0b2018-12-10 10:37:42 -070013#include <sound.h>
14#include <asm/gpio.h>
15#include <asm/io.h>
Simon Glass0ab6f0b2018-12-10 10:37:42 -070016#include "maxim_codec.h"
17
18/*
19 * Writes value to a device register through i2c
20 *
21 * @param priv Private data for driver
22 * @param reg reg number to be write
23 * @param data data to be writen to the above registor
24 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010025 * Return: int value 1 for change, 0 for no change or negative error code.
Simon Glass0ab6f0b2018-12-10 10:37:42 -070026 */
27int maxim_i2c_write(struct maxim_priv *priv, unsigned int reg,
28 unsigned char data)
29{
30 debug("%s: Write Addr : 0x%02X, Data : 0x%02X\n",
31 __func__, reg, data);
32 return dm_i2c_write(priv->dev, reg, &data, 1);
33}
34
35/*
36 * Read a value from a device register through i2c
37 *
38 * @param priv Private data for driver
39 * @param reg reg number to be read
40 * @param data address of read data to be stored
41 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010042 * Return: int value 0 for success, -1 in case of error.
Simon Glass0ab6f0b2018-12-10 10:37:42 -070043 */
44unsigned int maxim_i2c_read(struct maxim_priv *priv, unsigned int reg,
45 unsigned char *data)
46{
47 int ret;
48
49 return dm_i2c_read(priv->dev, reg, data, 1);
50 if (ret != 0) {
51 debug("%s: Error while reading register %#04x\n",
52 __func__, reg);
53 return -1;
54 }
55
56 return 0;
57}
58
59/*
60 * update device register bits through i2c
61 *
62 * @param priv Private data for driver
63 * @param reg codec register
64 * @param mask register mask
65 * @param value new value
66 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010067 * Return: int value 0 for success, non-zero error code.
Simon Glass0ab6f0b2018-12-10 10:37:42 -070068 */
69int maxim_bic_or(struct maxim_priv *priv, unsigned int reg, unsigned char mask,
70 unsigned char value)
71{
72 int change, ret = 0;
73 unsigned char old, new;
74
75 if (maxim_i2c_read(priv, reg, &old) != 0)
76 return -1;
77 new = (old & ~mask) | (value & mask);
78 change = (old != new) ? 1 : 0;
79 if (change)
80 ret = maxim_i2c_write(priv, reg, new);
81 if (ret < 0)
82 return ret;
83
84 return change;
85}