blob: 299d3de73fd853802eade7ad2da845e60bb823b4 [file] [log] [blame]
Kenneth Heitkeee44ade2012-02-08 13:45:33 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef _LINUX_SPMI_H
14#define _LINUX_SPMI_H
15
16#include <linux/types.h>
17#include <linux/device.h>
18#include <linux/mod_devicetable.h>
19
20/* Maximum slave identifier */
21#define SPMI_MAX_SLAVE_ID 16
22
23/* SPMI Commands */
24enum spmi_commands {
25 SPMI_CMD_EXT_WRITE = 0x00,
26 SPMI_CMD_RESET = 0x10,
27 SPMI_CMD_SLEEP = 0x11,
28 SPMI_CMD_SHUTDOWN = 0x12,
29 SPMI_CMD_WAKEUP = 0x13,
30 SPMI_CMD_AUTHENTICATE = 0x14,
31 SPMI_CMD_MSTR_READ = 0x15,
32 SPMI_CMD_MSTR_WRITE = 0x16,
33 SPMI_CMD_TRANSFER_BUS_OWNERSHIP = 0x1A,
34 SPMI_CMD_DDB_MASTER_READ = 0x1B,
35 SPMI_CMD_DDB_SLAVE_READ = 0x1C,
36 SPMI_CMD_EXT_READ = 0x20,
37 SPMI_CMD_EXT_WRITEL = 0x30,
38 SPMI_CMD_EXT_READL = 0x38,
39 SPMI_CMD_WRITE = 0x40,
40 SPMI_CMD_READ = 0x60,
41 SPMI_CMD_ZERO_WRITE = 0x80,
42};
43
44struct spmi_device;
45
46/**
47 * struct spmi_controller: interface to the SPMI master controller
48 * @nr: board-specific number identifier for this controller/bus
49 * @name: name for this controller
50 * @cmd: sends a non-data command sequence on the SPMI bus.
51 * @read_cmd: sends a register read command sequence on the SPMI bus.
52 * @write_cmd: sends a register write command sequence on the SPMI bus.
53 */
54struct spmi_controller {
55 struct device dev;
56 unsigned int nr;
57 struct list_head list;
58 int (*cmd)(struct spmi_controller *, u8 opcode, u8 sid);
59 int (*read_cmd)(struct spmi_controller *,
60 u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf);
61 int (*write_cmd)(struct spmi_controller *,
62 u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf);
63};
64#define to_spmi_controller(d) container_of(d, struct spmi_controller, dev)
65
66/**
67 * struct spmi_driver: Manage SPMI generic/slave device driver
68 * @probe: binds this driver to a SPMI device.
69 * @remove: unbinds this driver from the SPMI device.
70 * @shutdown: standard shutdown callback used during powerdown/halt.
71 * @suspend: standard suspend callback used during system suspend
72 * @resume: standard resume callback used during system resume
73 * @driver: SPMI device drivers should initialize name and owner field of
74 * this structure
75 * @id_table: list of SPMI devices supported by this driver
76 */
77struct spmi_driver {
78 int (*probe)(struct spmi_device *dev);
79 int (*remove)(struct spmi_device *dev);
80 void (*shutdown)(struct spmi_device *dev);
81 int (*suspend)(struct spmi_device *dev,
82 pm_message_t pmesg);
83 int (*resume)(struct spmi_device *dev);
84
85 struct device_driver driver;
86 const struct spmi_device_id *id_table;
87};
88#define to_spmi_driver(d) container_of(d, struct spmi_driver, driver)
89
90/**
Michael Bohan86622b32012-02-08 16:59:00 -080091 * struct spmi_resource: spmi_resource for one device_node
92 * @num_resources: number of resources for this device node
93 * @resources: array of resources for this device_node
94 * @of_node: device_node of the resource in question
Michael Bohan8d6aae32012-05-22 15:41:06 -070095 * @label: name used to reference the device from the driver
96 *
97 * Note that we explicitly add a 'label' pointer here since per
98 * the ePAPR 2.2.2, the device_node->name should be generic and not
99 * reflect precise programming model. Thus label enables a
100 * platform specific name to be assigned with the 'label' binding to
101 * allow for unique query names.
Michael Bohan86622b32012-02-08 16:59:00 -0800102 */
103struct spmi_resource {
104 struct resource *resource;
105 u32 num_resources;
106 struct device_node *of_node;
Michael Bohan8d6aae32012-05-22 15:41:06 -0700107 const char *label;
Michael Bohan86622b32012-02-08 16:59:00 -0800108};
109
110/**
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700111 * Client/device handle (struct spmi_device):
112 * ------------------------------------------
Michael Bohan86622b32012-02-08 16:59:00 -0800113 * This is the client/device handle returned when a SPMI device
114 * is registered with a controller.
115 * Pointer to this structure is used by client-driver as a handle.
116 * @dev: Driver model representation of the device.
117 * @name: Name of driver to use with this device.
118 * @ctrl: SPMI controller managing the bus hosting this device.
119 * @dev_node: array of SPMI resources - one entry per device_node.
120 * @num_dev_node: number of device_node structures.
121 * @sid: Slave Identifier.
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700122 */
123struct spmi_device {
124 struct device dev;
125 const char *name;
126 struct spmi_controller *ctrl;
Michael Bohan86622b32012-02-08 16:59:00 -0800127 struct spmi_resource *dev_node;
128 u32 num_dev_node;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700129 u8 sid;
130};
131#define to_spmi_device(d) container_of(d, struct spmi_device, dev)
132
133/**
134 * struct spmi_boardinfo: Declare board info for SPMI device bringup.
135 * @slave_id: slave identifier.
136 * @spmi_device: device to be registered with the SPMI framework.
137 * @of_node: pointer to the OpenFirmware device node.
Michael Bohan86622b32012-02-08 16:59:00 -0800138 * @dev_node: one spmi_resource for each device_node.
139 * @num_dev_node: number of device_node structures.
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700140 * @platform_data: goes to spmi_device.dev.platform_data
141 */
142struct spmi_boardinfo {
143 char name[SPMI_NAME_SIZE];
144 uint8_t slave_id;
145 struct device_node *of_node;
Michael Bohan86622b32012-02-08 16:59:00 -0800146 struct spmi_resource *dev_node;
147 u32 num_dev_node;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700148 const void *platform_data;
149};
150
151/**
152 * spmi_driver_register: Client driver registration with SPMI framework.
153 * @drv: client driver to be associated with client-device.
154 *
155 * This API will register the client driver with the SPMI framework.
156 * It is called from the driver's module-init function.
157 */
158extern int spmi_driver_register(struct spmi_driver *drv);
159
160/**
161 * spmi_driver_unregister - reverse effect of spmi_driver_register
162 * @sdrv: the driver to unregister
163 * Context: can sleep
164 */
165static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
166{
167 if (sdrv)
168 driver_unregister(&sdrv->driver);
169}
170
171/**
172 * spmi_add_controller: Controller bring-up.
173 * @ctrl: controller to be registered.
174 *
175 * A controller is registered with the framework using this API. ctrl->nr is the
176 * desired number with which SPMI framework registers the controller.
177 * Function will return -EBUSY if the number is in use.
178 */
179extern int spmi_add_controller(struct spmi_controller *ctrl);
180
181/**
182 * spmi_del_controller: Controller tear-down.
183 * Controller added with the above API is teared down using this API.
184 */
185extern int spmi_del_controller(struct spmi_controller *ctrl);
186
187/**
188 * spmi_busnum_to_ctrl: Map bus number to controller
189 * @busnum: bus number
190 *
191 * Returns controller device representing this bus number
192 */
193extern struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num);
194
195/**
196 * spmi_alloc_device: Allocate a new SPMI devices.
197 * @ctrl: controller to which this device is to be added to.
198 * Context: can sleep
199 *
200 * Allows a driver to allocate and initialize a SPMI device without
201 * registering it immediately. This allows a driver to directly fill
202 * the spmi_device structure before calling spmi_add_device().
203 *
204 * Caller is responsible to call spmi_add_device() on the returned
205 * spmi_device. If the caller needs to discard the spmi_device without
206 * adding it, then spmi_dev_put() should be called.
207 */
208extern struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl);
209
210/**
211 * spmi_add_device: Add spmi_device allocated with spmi_alloc_device().
212 * @spmi_dev: spmi_device to be added (registered).
213 */
214extern int spmi_add_device(struct spmi_device *spmi_dev);
215
216/**
217 * spmi_new_device: Instantiates a new SPMI device
218 * @ctrl: controller to which this device is to be added to.
219 * @info: board information for this device.
220 *
221 * Returns the new device or NULL.
222 */
223extern struct spmi_device *spmi_new_device(struct spmi_controller *ctrl,
224 struct spmi_boardinfo const *info);
225
226/* spmi_remove_device: Remove the effect of spmi_add_device() */
227extern void spmi_remove_device(struct spmi_device *spmi_dev);
228
229#ifdef CONFIG_SPMI
230/**
231 * spmi_register_board_info: Board-initialization routine.
232 * @bus_num: controller number (bus) on which this device will sit.
233 * @info: list of all devices on all controllers present on the board.
234 * @n: number of entries.
235 *
236 * API enumerates respective devices on corresponding controller.
237 * Called from board-init function.
238 */
239extern int spmi_register_board_info(int busnum,
240 struct spmi_boardinfo const *info, unsigned n);
241#else
242static inline int spmi_register_board_info(int busnum,
243 struct spmi_boardinfo const *info, unsigned n)
244{
245 return 0;
246}
247#endif
248
249static inline void *spmi_get_ctrldata(const struct spmi_controller *ctrl)
250{
251 return dev_get_drvdata(&ctrl->dev);
252}
253
254static inline void spmi_set_ctrldata(struct spmi_controller *ctrl, void *data)
255{
256 dev_set_drvdata(&ctrl->dev, data);
257}
258
259static inline void *spmi_get_devicedata(const struct spmi_device *dev)
260{
261 return dev_get_drvdata(&dev->dev);
262}
263
264static inline void spmi_set_devicedata(struct spmi_device *dev, void *data)
265{
266 dev_set_drvdata(&dev->dev, data);
267}
268
269static inline void spmi_dev_put(struct spmi_device *spmidev)
270{
271 if (spmidev)
272 put_device(&spmidev->dev);
273}
274
275/**
276 * spmi_register_read() - register read
277 * @ctrl: SPMI controller.
278 * @sid: slave identifier.
279 * @ad: slave register address (5-bit address).
280 * @buf: buffer to be populated with data from the Slave.
281 *
282 * Reads 1 byte of data from a Slave device register.
283 */
284extern int spmi_register_read(struct spmi_controller *ctrl,
285 u8 sid, u8 ad, u8 *buf);
286
287/**
288 * spmi_ext_register_read() - extended register read
289 * @ctrl: SPMI controller.
290 * @sid: slave identifier.
291 * @ad: slave register address (8-bit address).
292 * @len: the request number of bytes to read (up to 16 bytes).
293 * @buf: buffer to be populated with data from the Slave.
294 *
295 * Reads up to 16 bytes of data from the extended register space on a
296 * Slave device.
297 */
298extern int spmi_ext_register_read(struct spmi_controller *ctrl,
299 u8 sid, u8 ad, u8 *buf, int len);
300
301/**
302 * spmi_ext_register_readl() - extended register read long
303 * @ctrl: SPMI controller.
304 * @sid: slave identifier.
305 * @ad: slave register address (16-bit address).
306 * @len: the request number of bytes to read (up to 8 bytes).
307 * @buf: buffer to be populated with data from the Slave.
308 *
309 * Reads up to 8 bytes of data from the extended register space on a
310 * Slave device using 16-bit address.
311 */
312extern int spmi_ext_register_readl(struct spmi_controller *ctrl,
313 u8 sid, u16 ad, u8 *buf, int len);
314
315/**
316 * spmi_register_write() - register write
317 * @ctrl: SPMI controller.
318 * @sid: slave identifier.
319 * @ad: slave register address (5-bit address).
320 * @buf: buffer containing the data to be transferred to the Slave.
321 *
322 * Writes 1 byte of data to a Slave device register.
323 */
324extern int spmi_register_write(struct spmi_controller *ctrl,
325 u8 sid, u8 ad, u8 *buf);
326
327/**
328 * spmi_register_zero_write() - register zero write
329 * @ctrl: SPMI controller.
330 * @sid: slave identifier.
331 * @data: the data to be written to register 0 (7-bits).
332 *
333 * Writes data to register 0 of the Slave device.
334 */
335extern int spmi_register_zero_write(struct spmi_controller *ctrl,
336 u8 sid, u8 data);
337
338/**
339 * spmi_ext_register_write() - extended register write
340 * @ctrl: SPMI controller.
341 * @sid: slave identifier.
342 * @ad: slave register address (8-bit address).
343 * @buf: buffer containing the data to be transferred to the Slave.
344 * @len: the request number of bytes to read (up to 16 bytes).
345 *
346 * Writes up to 16 bytes of data to the extended register space of a
347 * Slave device.
348 */
349extern int spmi_ext_register_write(struct spmi_controller *ctrl,
350 u8 sid, u8 ad, u8 *buf, int len);
351
352/**
353 * spmi_ext_register_writel() - extended register write long
354 * @ctrl: SPMI controller.
355 * @sid: slave identifier.
356 * @ad: slave register address (16-bit address).
357 * @buf: buffer containing the data to be transferred to the Slave.
358 * @len: the request number of bytes to read (up to 8 bytes).
359 *
360 * Writes up to 8 bytes of data to the extended register space of a
361 * Slave device using 16-bit address.
362 */
363extern int spmi_ext_register_writel(struct spmi_controller *ctrl,
364 u8 sid, u16 ad, u8 *buf, int len);
365
366/**
367 * spmi_command_reset() - sends RESET command to the specified slave
368 * @ctrl: SPMI controller.
369 * @sid: slave identifier.
370 *
371 * The Reset command initializes the Slave and forces all registers to
372 * their reset values. The Slave shall enter the STARTUP state after
373 * receiving a Reset command.
374 *
375 * Returns
376 * -EINVAL for invalid slave identifier.
377 * -EPERM if the SPMI transaction is denied due to permission issues.
378 * -EIO if the SPMI transaction fails (parity errors, etc).
379 * -ETIMEDOUT if the SPMI transaction times out.
380 */
381extern int spmi_command_reset(struct spmi_controller *ctrl, u8 sid);
382
383/**
384 * spmi_command_sleep() - sends SLEEP command to the specified slave
385 * @ctrl: SPMI controller.
386 * @sid: slave identifier.
387 *
388 * The Sleep command causes the Slave to enter the user defined SLEEP state.
389 *
390 * Returns
391 * -EINVAL for invalid slave identifier.
392 * -EPERM if the SPMI transaction is denied due to permission issues.
393 * -EIO if the SPMI transaction fails (parity errors, etc).
394 * -ETIMEDOUT if the SPMI transaction times out.
395 */
396extern int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid);
397
398/**
399 * spmi_command_wakeup() - sends WAKEUP command to the specified slave
400 * @ctrl: SPMI controller.
401 * @sid: slave identifier.
402 *
403 * The Wakeup command causes the Slave to move from the SLEEP state to
404 * the ACTIVE state.
405 *
406 * Returns
407 * -EINVAL for invalid slave identifier.
408 * -EPERM if the SPMI transaction is denied due to permission issues.
409 * -EIO if the SPMI transaction fails (parity errors, etc).
410 * -ETIMEDOUT if the SPMI transaction times out.
411 */
412extern int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid);
413
414/**
415 * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave
416 * @ctrl: SPMI controller.
417 * @sid: slave identifier.
418 *
419 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
420 *
421 * Returns
422 * -EINVAL for invalid slave identifier.
423 * -EPERM if the SPMI transaction is denied due to permission issues.
424 * -EIO if the SPMI transaction fails (parity errors, etc).
425 * -ETIMEDOUT if the SPMI transaction times out.
426 */
427extern int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid);
Michael Bohan08e1e902012-05-23 10:55:22 -0700428
Michael Bohan0e5534d2012-05-22 17:33:45 -0700429/**
430 * spmi_for_each_container_dev - iterate over the array of devnode resources.
431 * @res: spmi_resource pointer used as the array cursor
432 * @spmi_dev: spmi_device to iterate
433 *
434 * Only useable in spmi-dev-container configurations.
435 */
436#define spmi_for_each_container_dev(res, spmi_dev) \
437 for (res = ((spmi_dev)->dev_node ? &(spmi_dev)->dev_node[0] : NULL); \
438 (res - (spmi_dev)->dev_node) < (spmi_dev)->num_dev_node; res++)
Michael Bohan08e1e902012-05-23 10:55:22 -0700439
Michael Bohan0e5534d2012-05-22 17:33:45 -0700440extern struct resource *spmi_get_resource(struct spmi_device *dev,
441 struct spmi_resource *node,
442 unsigned int type, unsigned int res_num);
443
Michael Bohana6a354d2012-05-21 17:47:11 -0700444struct resource *spmi_get_resource_byname(struct spmi_device *dev,
445 struct spmi_resource *node,
446 unsigned int type,
447 const char *name);
448
Michael Bohan0e5534d2012-05-22 17:33:45 -0700449extern int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node,
Michael Bohan08e1e902012-05-23 10:55:22 -0700450 unsigned int res_num);
Michael Bohana6a354d2012-05-21 17:47:11 -0700451
452extern int spmi_get_irq_byname(struct spmi_device *dev,
453 struct spmi_resource *node, const char *name);
Michael Bohan8d6aae32012-05-22 15:41:06 -0700454
455struct spmi_resource *spmi_get_dev_container_byname(struct spmi_device *dev,
456 const char *label);
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700457#endif