blob: ead1b167e8ebc106583ebae384ccfbdf8e6e0c04 [file] [log] [blame]
Jeremy Sollere20c47b2021-01-26 13:21:12 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <console/console.h>
Tim Crawfordca115452024-03-07 15:21:59 -07004#include <device/i2c_bus.h>
Jeremy Sollere20c47b2021-01-26 13:21:12 -07005#include "chip.h"
6#include "tas5825m.h"
7
8int tas5825m_write_at(struct device *dev, uint8_t addr, uint8_t value)
9{
Tim Crawfordca115452024-03-07 15:21:59 -070010 return i2c_dev_writeb_at(dev, addr, value);
Jeremy Sollere20c47b2021-01-26 13:21:12 -070011}
12
Jeremy Sollere20c47b2021-01-26 13:21:12 -070013int tas5825m_write_block_at(struct device *dev, uint8_t addr,
14 const uint8_t *values, uint8_t length)
15{
Tim Crawfordca115452024-03-07 15:21:59 -070016 // TODO: use I2C block write for better performance; SMBus does not
17 // have `transfer` op for it.
18
Jeremy Sollere20c47b2021-01-26 13:21:12 -070019 int res = 0;
20 for (uint8_t i = 0; i < length; i++) {
Tim Crawfordca115452024-03-07 15:21:59 -070021 res = i2c_dev_writeb_at(dev, addr + i, values[i]);
Jeremy Sollere20c47b2021-01-26 13:21:12 -070022 if (res < 0)
23 return res;
24 }
25 return (int)length;
26}
27
28int tas5825m_set_page(struct device *dev, uint8_t page)
29{
30 return tas5825m_write_at(dev, 0x00, page);
31}
32
33int tas5825m_set_book(struct device *dev, uint8_t book)
34{
35 int res = tas5825m_set_page(dev, 0x00);
36 if (res < 0)
37 return res;
38 return tas5825m_write_at(dev, 0x7F, book);
39}
40
41__weak int tas5825m_setup(struct device *dev, int id)
42{
43 printk(BIOS_ERR, "tas5825m: setup not implemented\n");
44 return -1;
45}
46
47static void tas5825m_init(struct device *dev)
48{
Tim Crawfordca115452024-03-07 15:21:59 -070049 if (dev->enabled && dev->path.type == DEVICE_PATH_I2C && i2c_link(dev)) {
Jeremy Sollere20c47b2021-01-26 13:21:12 -070050 printk(BIOS_DEBUG, "tas5825m at %s\n", dev_path(dev));
51
52 struct drivers_i2c_tas5825m_config *config = dev->chip_info;
53 if (config) {
54 printk(BIOS_DEBUG, "tas5825m id %d\n", config->id);
55 int res = tas5825m_setup(dev, config->id);
56 if (res)
57 printk(BIOS_ERR, "tas5825m init failed: %d\n", res);
58 else
59 printk(BIOS_DEBUG, "tas5825m init successful\n");
60 } else {
61 printk(BIOS_ERR, "tas5825m: failed to find config\n");
62 }
63 }
64}
65
66static struct device_operations tas5825m_operations = {
67 .read_resources = noop_read_resources,
68 .set_resources = noop_set_resources,
69 .init = tas5825m_init,
70};
71
72static void tas5825m_enable_dev(struct device *dev)
73{
74 dev->ops = &tas5825m_operations;
75}
76
77struct chip_operations drivers_i2c_tas5825m_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +090078 .name = "TI TAS5825M Amplifier",
Jeremy Sollere20c47b2021-01-26 13:21:12 -070079 .enable_dev = tas5825m_enable_dev,
80};