blob: cb4c491078c2c9eb369fbfb662c734218b776d7c [file] [log] [blame]
Thomas Gleixner80503b22019-05-24 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lars-Peter Clausen6b109982014-06-06 14:09:17 +02002/*
3 * Load Analog Devices SigmaStudio firmware files
4 *
5 * Copyright 2009-2011 Analog Devices Inc.
Lars-Peter Clausen6b109982014-06-06 14:09:17 +02006 */
7
Lars-Peter Clausen6b109982014-06-06 14:09:17 +02008#include <linux/export.h>
Lars-Peter Clausend48b0882014-11-19 18:29:05 +01009#include <linux/i2c.h>
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020010#include <linux/module.h>
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010011#include <linux/slab.h>
12#include <asm/unaligned.h>
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020013
14#include "sigmadsp.h"
15
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010016static int sigmadsp_write_i2c(void *control_data,
17 unsigned int addr, const uint8_t data[], size_t len)
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020018{
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010019 uint8_t *buf;
20 int ret;
21
22 buf = kzalloc(2 + len, GFP_KERNEL | GFP_DMA);
23 if (!buf)
24 return -ENOMEM;
25
26 put_unaligned_be16(addr, buf);
27 memcpy(buf + 2, data, len);
28
29 ret = i2c_master_send(control_data, buf, len + 2);
30
31 kfree(buf);
32
Pascal Huerst61c4a1a2016-02-10 15:59:28 +010033 if (ret < 0)
34 return ret;
35
36 return 0;
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020037}
38
Lars-Peter Clausena35daac2014-11-19 18:29:06 +010039static int sigmadsp_read_i2c(void *control_data,
40 unsigned int addr, uint8_t data[], size_t len)
41{
42 struct i2c_client *client = control_data;
43 struct i2c_msg msgs[2];
44 uint8_t buf[2];
45 int ret;
46
47 put_unaligned_be16(addr, buf);
48
49 msgs[0].addr = client->addr;
50 msgs[0].len = sizeof(buf);
51 msgs[0].buf = buf;
52 msgs[0].flags = 0;
53
54 msgs[1].addr = client->addr;
55 msgs[1].len = len;
56 msgs[1].buf = data;
57 msgs[1].flags = I2C_M_RD;
58
59 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
60 if (ret < 0)
61 return ret;
62 else if (ret != ARRAY_SIZE(msgs))
63 return -EIO;
64 return 0;
65}
66
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010067/**
68 * devm_sigmadsp_init_i2c() - Initialize SigmaDSP instance
69 * @client: The parent I2C device
70 * @ops: The sigmadsp_ops to use for this instance
71 * @firmware_name: Name of the firmware file to load
72 *
73 * Allocates a SigmaDSP instance and loads the specified firmware file.
74 *
75 * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
76 */
77struct sigmadsp *devm_sigmadsp_init_i2c(struct i2c_client *client,
78 const struct sigmadsp_ops *ops, const char *firmware_name)
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020079{
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010080 struct sigmadsp *sigmadsp;
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020081
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010082 sigmadsp = devm_sigmadsp_init(&client->dev, ops, firmware_name);
83 if (IS_ERR(sigmadsp))
84 return sigmadsp;
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020085
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010086 sigmadsp->control_data = client;
87 sigmadsp->write = sigmadsp_write_i2c;
Lars-Peter Clausena35daac2014-11-19 18:29:06 +010088 sigmadsp->read = sigmadsp_read_i2c;
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010089
90 return sigmadsp;
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020091}
Lars-Peter Clausend48b0882014-11-19 18:29:05 +010092EXPORT_SYMBOL_GPL(devm_sigmadsp_init_i2c);
Lars-Peter Clausen6b109982014-06-06 14:09:17 +020093
94MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
95MODULE_DESCRIPTION("SigmaDSP I2C firmware loader");
96MODULE_LICENSE("GPL");