1/* Copyright (c) 2012-2013, The Linux Foundation. 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#ifndef _LINUX_SPMI_H
13#define _LINUX_SPMI_H
14
15#include <linux/types.h>
16#include <linux/device.h>
17#include <linux/mod_devicetable.h>
18
19/* Maximum slave identifier */
20#define SPMI_MAX_SLAVE_ID 16
21
22/* SPMI Commands */
23#define SPMI_CMD_EXT_WRITE 0x00
24#define SPMI_CMD_RESET 0x10
25#define SPMI_CMD_SLEEP 0x11
26#define SPMI_CMD_SHUTDOWN 0x12
27#define SPMI_CMD_WAKEUP 0x13
28#define SPMI_CMD_AUTHENTICATE 0x14
29#define SPMI_CMD_MSTR_READ 0x15
30#define SPMI_CMD_MSTR_WRITE 0x16
31#define SPMI_CMD_TRANSFER_BUS_OWNERSHIP 0x1A
32#define SPMI_CMD_DDB_MASTER_READ 0x1B
33#define SPMI_CMD_DDB_SLAVE_READ 0x1C
34#define SPMI_CMD_EXT_READ 0x20
35#define SPMI_CMD_EXT_WRITEL 0x30
36#define SPMI_CMD_EXT_READL 0x38
37#define SPMI_CMD_WRITE 0x40
38#define SPMI_CMD_READ 0x60
39#define SPMI_CMD_ZERO_WRITE 0x80
40
41/**
42 * struct spmi_device - Basic representation of an SPMI device
43 * @dev: Driver model representation of the device.
44 * @ctrl: SPMI controller managing the bus hosting this device.
45 * @usid: This devices' Unique Slave IDentifier.
46 */
47struct spmi_device {
48 struct device dev;
49 struct spmi_controller *ctrl;
50 u8 usid;
51};
52
53static inline struct spmi_device *to_spmi_device(struct device *d)
54{
55 return container_of(d, struct spmi_device, dev);
56}
57
58static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev)
59{
60 return dev_get_drvdata(&sdev->dev);
61}
62
63static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data)
64{
65 dev_set_drvdata(&sdev->dev, data);
66}
67
68struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl);
69
70static inline void spmi_device_put(struct spmi_device *sdev)
71{
72 if (sdev)
73 put_device(&sdev->dev);
74}
75
76int spmi_device_add(struct spmi_device *sdev);
77
78void spmi_device_remove(struct spmi_device *sdev);
79
80/**
81 * struct spmi_controller - interface to the SPMI master controller
82 * @dev: Driver model representation of the device.
83 * @nr: board-specific number identifier for this controller/bus
84 * @cmd: sends a non-data command sequence on the SPMI bus.
85 * @read_cmd: sends a register read command sequence on the SPMI bus.
86 * @write_cmd: sends a register write command sequence on the SPMI bus.
87 */
88struct spmi_controller {
89 struct device dev;
90 unsigned int nr;
91 int (*cmd)(struct spmi_controller *ctrl, u8 opcode, u8 sid);
92 int (*read_cmd)(struct spmi_controller *ctrl, u8 opcode,
93 u8 sid, u16 addr, u8 *buf, size_t len);
94 int (*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
95 u8 sid, u16 addr, const u8 *buf, size_t len);
96};
97
98static inline struct spmi_controller *to_spmi_controller(struct device *d)
99{
100 return container_of(d, struct spmi_controller, dev);
101}
102
103static inline
104void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl)
105{
106 return dev_get_drvdata(&ctrl->dev);
107}
108
109static inline void spmi_controller_set_drvdata(struct spmi_controller *ctrl,
110 void *data)
111{
112 dev_set_drvdata(&ctrl->dev, data);
113}
114
115struct spmi_controller *spmi_controller_alloc(struct device *parent,
116 size_t size);
117
118/**
119 * spmi_controller_put() - decrement controller refcount
120 * @ctrl SPMI controller.
121 */
122static inline void spmi_controller_put(struct spmi_controller *ctrl)
123{
124 if (ctrl)
125 put_device(&ctrl->dev);
126}
127
128int spmi_controller_add(struct spmi_controller *ctrl);
129void spmi_controller_remove(struct spmi_controller *ctrl);
130
131/**
132 * struct spmi_driver - SPMI slave device driver
133 * @driver: SPMI device drivers should initialize name and owner field of
134 * this structure.
135 * @probe: binds this driver to a SPMI device.
136 * @remove: unbinds this driver from the SPMI device.
137 *
138 * If PM runtime support is desired for a slave, a device driver can call
139 * pm_runtime_put() from their probe() routine (and a balancing
140 * pm_runtime_get() in remove()). PM runtime support for a slave is
141 * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
142 * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP
143 * command is sent to the slave to bring it back to ACTIVE.
144 */
145struct spmi_driver {
146 struct device_driver driver;
147 int (*probe)(struct spmi_device *sdev);
148 void (*remove)(struct spmi_device *sdev);
149};
150
151static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
152{
153 return container_of(d, struct spmi_driver, driver);
154}
155
156#define spmi_driver_register(sdrv) \
157 __spmi_driver_register(sdrv, THIS_MODULE)
158int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner);
159
160/**
161 * spmi_driver_unregister() - unregister an SPMI client driver
162 * @sdrv: the driver to unregister
163 */
164static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
165{
166 if (sdrv)
167 driver_unregister(&sdrv->driver);
168}
169
170#define module_spmi_driver(__spmi_driver) \
171 module_driver(__spmi_driver, spmi_driver_register, \
172 spmi_driver_unregister)
173
174int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
175int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
176 size_t len);
177int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
178 size_t len);
179int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
180int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
181int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
182 const u8 *buf, size_t len);
183int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
184 const u8 *buf, size_t len);
185int spmi_command_reset(struct spmi_device *sdev);
186int spmi_command_sleep(struct spmi_device *sdev);
187int spmi_command_wakeup(struct spmi_device *sdev);
188int spmi_command_shutdown(struct spmi_device *sdev);
189
190#endif
191